001/** 002 * 003 * Copyright 2020 Paul Schaub 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smackx.urldata.http.element; 018 019import java.util.ArrayList; 020import java.util.List; 021 022import org.jivesoftware.smack.packet.XmlEnvironment; 023import org.jivesoftware.smack.util.EqualsUtil; 024import org.jivesoftware.smack.util.HashCode; 025import org.jivesoftware.smack.util.XmlStringBuilder; 026 027import org.jivesoftware.smackx.urldata.element.MetaInformationElement; 028 029public final class HttpAuthElement implements MetaInformationElement { 030 031 public static final String ELEMENT = "auth"; 032 public static final String PREFIX = "http"; 033 public static final String ATTR_SCHEME = "scheme"; 034 035 public static final String SCHEME_BASIC = "basic"; 036 037 private final String scheme; 038 private final List<AuthParamElement> params = new ArrayList<>(); 039 040 public HttpAuthElement(String scheme, List<AuthParamElement> params) { 041 this.scheme = scheme; 042 if (params != null) { 043 this.params.addAll(params); 044 } 045 } 046 047 public static HttpAuthElement basicAuth() { 048 return basicAuth(null, null); 049 } 050 051 public static HttpAuthElement basicAuth(String username, String password) { 052 return basicAuth(null, username, password); 053 } 054 055 public static HttpAuthElement basicAuth(String realm, String username, String password) { 056 List<AuthParamElement> params = new ArrayList<>(); 057 if (realm != null) { 058 params.add(AuthParamElement.realm(realm)); 059 } 060 if (username != null) { 061 params.add(AuthParamElement.username(username)); 062 } 063 if (password != null) { 064 params.add(AuthParamElement.password(password)); 065 } 066 067 return new HttpAuthElement(SCHEME_BASIC, params); 068 } 069 070 public String getScheme() { 071 return scheme; 072 } 073 074 public List<AuthParamElement> getParams() { 075 return params; 076 } 077 078 @Override 079 public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) { 080 XmlStringBuilder sb = new XmlStringBuilder(this) 081 .attribute(ATTR_SCHEME, getScheme()); 082 if (getParams().isEmpty()) { 083 return sb.closeEmptyElement(); 084 } else { 085 return sb.rightAngleBracket() 086 .append(getParams()) 087 .closeElement(this); 088 } 089 } 090 091 @Override 092 public String getElementName() { 093 return PREFIX + ':' + ELEMENT; 094 } 095 096 public AuthParamElement getParam(String name) { 097 for (AuthParamElement param : getParams()) { 098 if (param.getName().equals(name)) { 099 return param; 100 } 101 } 102 return null; 103 } 104 105 @Override 106 public int hashCode() { 107 return HashCode.builder() 108 .append(getElementName()) 109 .append(getScheme()) 110 .append(getParams()) 111 .build(); 112 } 113 114 @Override 115 public boolean equals(Object obj) { 116 return EqualsUtil.equals(this, obj, (equalsBuilder, other) -> 117 equalsBuilder 118 .append(getElementName(), other.getElementName()) 119 .append(getScheme(), other.getScheme()) 120 .append(getParams(), other.getParams())); 121 } 122 123}