AuthParamElement.java

  1. /**
  2.  *
  3.  * Copyright 2020 Paul Schaub
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.urldata.http.element;

  18. import org.jivesoftware.smack.packet.XmlEnvironment;
  19. import org.jivesoftware.smack.util.EqualsUtil;
  20. import org.jivesoftware.smack.util.HashCode;
  21. import org.jivesoftware.smack.util.XmlStringBuilder;

  22. public class AuthParamElement extends NameValuePairElement {

  23.     public static final String ELEMENT = "auth-param";
  24.     public static final String PREFIX = "http";

  25.     public static final String NAME_REALM = "realm";
  26.     public static final String NAME_USERNAME = "username";
  27.     public static final String NAME_PASSWORD = "password";

  28.     public AuthParamElement(String name, String value) {
  29.         super(name, value);
  30.     }

  31.     public static AuthParamElement realm(String realm) {
  32.         return new AuthParamElement(NAME_REALM, realm);
  33.     }

  34.     public static AuthParamElement username(String username) {
  35.         return new AuthParamElement(NAME_USERNAME, username);
  36.     }

  37.     public static AuthParamElement password(String password) {
  38.         return new AuthParamElement(NAME_PASSWORD, password);
  39.     }

  40.     @Override
  41.     public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
  42.         return addCommonXml(new XmlStringBuilder(this))
  43.                 .closeEmptyElement();
  44.     }

  45.     @Override
  46.     public String getElementName() {
  47.         return PREFIX + ':' + ELEMENT;
  48.     }

  49.     @Override
  50.     public int hashCode() {
  51.         return HashCode.builder()
  52.                 .append(getElementName())
  53.                 .append(getName())
  54.                 .append(getValue())
  55.                 .build();
  56.     }

  57.     @Override
  58.     public boolean equals(Object obj) {
  59.         return EqualsUtil.equals(this, obj, (equalsBuilder, other) ->
  60.                 equalsBuilder
  61.                         .append(getElementName(), other.getElementName())
  62.                         .append(getName(), other.getName())
  63.                         .append(getValue(), other.getValue()));
  64.     }
  65. }