HttpAuthElement.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 java.util.ArrayList;
  19. import java.util.List;

  20. import org.jivesoftware.smack.packet.XmlEnvironment;
  21. import org.jivesoftware.smack.util.EqualsUtil;
  22. import org.jivesoftware.smack.util.HashCode;
  23. import org.jivesoftware.smack.util.XmlStringBuilder;

  24. import org.jivesoftware.smackx.urldata.element.MetaInformationElement;

  25. public final class HttpAuthElement implements MetaInformationElement {

  26.     public static final String ELEMENT = "auth";
  27.     public static final String PREFIX = "http";
  28.     public static final String ATTR_SCHEME = "scheme";

  29.     public static final String SCHEME_BASIC = "basic";

  30.     private final String scheme;
  31.     private final List<AuthParamElement> params = new ArrayList<>();

  32.     public HttpAuthElement(String scheme, List<AuthParamElement> params) {
  33.         this.scheme = scheme;
  34.         if (params != null) {
  35.             this.params.addAll(params);
  36.         }
  37.     }

  38.     public static HttpAuthElement basicAuth() {
  39.         return basicAuth(null, null);
  40.     }

  41.     public static HttpAuthElement basicAuth(String username, String password) {
  42.         return basicAuth(null, username, password);
  43.     }

  44.     public static HttpAuthElement basicAuth(String realm, String username, String password) {
  45.         List<AuthParamElement> params = new ArrayList<>();
  46.         if (realm != null) {
  47.             params.add(AuthParamElement.realm(realm));
  48.         }
  49.         if (username != null) {
  50.             params.add(AuthParamElement.username(username));
  51.         }
  52.         if (password != null) {
  53.             params.add(AuthParamElement.password(password));
  54.         }

  55.         return new HttpAuthElement(SCHEME_BASIC, params);
  56.     }

  57.     public String getScheme() {
  58.         return scheme;
  59.     }

  60.     public List<AuthParamElement> getParams() {
  61.         return params;
  62.     }

  63.     @Override
  64.     public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
  65.         XmlStringBuilder sb = new XmlStringBuilder(this)
  66.                 .attribute(ATTR_SCHEME, getScheme());
  67.         if (getParams().isEmpty()) {
  68.             return sb.closeEmptyElement();
  69.         } else {
  70.             return sb.rightAngleBracket()
  71.                     .append(getParams())
  72.                     .closeElement(this);
  73.         }
  74.     }

  75.     @Override
  76.     public String getElementName() {
  77.         return PREFIX + ':' + ELEMENT;
  78.     }

  79.     public AuthParamElement getParam(String name) {
  80.         for (AuthParamElement param : getParams()) {
  81.             if (param.getName().equals(name)) {
  82.                 return param;
  83.             }
  84.         }
  85.         return null;
  86.     }

  87.     @Override
  88.     public int hashCode() {
  89.         return HashCode.builder()
  90.                 .append(getElementName())
  91.                 .append(getScheme())
  92.                 .append(getParams())
  93.                 .build();
  94.     }

  95.     @Override
  96.     public boolean equals(Object obj) {
  97.         return EqualsUtil.equals(this, obj, (equalsBuilder, other) ->
  98.                 equalsBuilder
  99.                         .append(getElementName(), other.getElementName())
  100.                         .append(getScheme(), other.getScheme())
  101.                         .append(getParams(), other.getParams()));
  102.     }

  103. }