| 1 | /** |
|---|
| 2 | * Copyright (C) 2009 EDIT |
|---|
| 3 | * European Distributed Institute of Taxonomy |
|---|
| 4 | * http://www.e-taxonomy.eu |
|---|
| 5 | * |
|---|
| 6 | * The contents of this file are subject to the Mozilla Public License Version 1.1 |
|---|
| 7 | * See LICENSE.TXT at the top of this package for the full license terms. |
|---|
| 8 | */ |
|---|
| 9 | |
|---|
| 10 | package eu.etaxonomy.cdm.model.common; |
|---|
| 11 | |
|---|
| 12 | import java.util.HashMap; |
|---|
| 13 | import java.util.Map; |
|---|
| 14 | |
|---|
| 15 | import javax.persistence.Entity; |
|---|
| 16 | import javax.persistence.FetchType; |
|---|
| 17 | import javax.persistence.Transient; |
|---|
| 18 | import javax.wsdl.Definition; |
|---|
| 19 | import javax.xml.bind.annotation.XmlAccessType; |
|---|
| 20 | import javax.xml.bind.annotation.XmlAccessorType; |
|---|
| 21 | import javax.xml.bind.annotation.XmlElement; |
|---|
| 22 | import javax.xml.bind.annotation.XmlRootElement; |
|---|
| 23 | import javax.xml.bind.annotation.XmlTransient; |
|---|
| 24 | import javax.xml.bind.annotation.XmlType; |
|---|
| 25 | import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; |
|---|
| 26 | |
|---|
| 27 | import org.hibernate.annotations.CollectionOfElements; |
|---|
| 28 | import org.hibernate.annotations.NaturalId; |
|---|
| 29 | import org.hibernate.annotations.Type; |
|---|
| 30 | import org.hibernate.annotations.TypeDef; |
|---|
| 31 | import org.hibernate.annotations.TypeDefs; |
|---|
| 32 | |
|---|
| 33 | import com.ibm.lsid.MalformedLSIDException; |
|---|
| 34 | |
|---|
| 35 | import eu.etaxonomy.cdm.hibernate.WSDLDefinitionUserType; |
|---|
| 36 | import eu.etaxonomy.cdm.jaxb.NamespacesAdapter; |
|---|
| 37 | |
|---|
| 38 | @XmlAccessorType(XmlAccessType.FIELD) |
|---|
| 39 | @XmlType(name = "LSIDAuthority", propOrder = { |
|---|
| 40 | "authority", |
|---|
| 41 | "server", |
|---|
| 42 | "port", |
|---|
| 43 | "url", |
|---|
| 44 | "namespaces" |
|---|
| 45 | }) |
|---|
| 46 | @XmlRootElement(name = "LSIDAuthority") |
|---|
| 47 | @Entity |
|---|
| 48 | @TypeDefs(@TypeDef(name="wsdlDefinitionUserType", typeClass=WSDLDefinitionUserType.class)) |
|---|
| 49 | public class LSIDAuthority extends CdmBase { |
|---|
| 50 | |
|---|
| 51 | /** |
|---|
| 52 | * |
|---|
| 53 | */ |
|---|
| 54 | private static final long serialVersionUID = 9168994979216936689L; |
|---|
| 55 | |
|---|
| 56 | public static final String AUTHORITY_ID_PREFIX = "lsidauth:"; |
|---|
| 57 | |
|---|
| 58 | private static final String AUTHORITY_PROTOCOL="http"; |
|---|
| 59 | private static final String AUTHORITY_PATH="/authority/"; |
|---|
| 60 | |
|---|
| 61 | @XmlElement(name = "Authority") |
|---|
| 62 | @NaturalId |
|---|
| 63 | private String authority; |
|---|
| 64 | |
|---|
| 65 | // the resolved components of the lsid |
|---|
| 66 | @XmlElement(name = "Server") |
|---|
| 67 | private String server; |
|---|
| 68 | @XmlElement(name = "Port") |
|---|
| 69 | private int port = -1; |
|---|
| 70 | @XmlElement(name = "Url") |
|---|
| 71 | private String url; |
|---|
| 72 | |
|---|
| 73 | // the wsdl describing how to invoke operations on the authority |
|---|
| 74 | @XmlTransient |
|---|
| 75 | @Type(type = "wsdlDefinitionUserType") |
|---|
| 76 | private Definition authorityWSDL; |
|---|
| 77 | |
|---|
| 78 | @XmlElement(name = "Namespaces") |
|---|
| 79 | @XmlJavaTypeAdapter(NamespacesAdapter.class) |
|---|
| 80 | @CollectionOfElements(fetch = FetchType.LAZY) |
|---|
| 81 | private Map<String,Class<? extends IIdentifiableEntity>> namespaces = new HashMap<String,Class<? extends IIdentifiableEntity>>(); |
|---|
| 82 | |
|---|
| 83 | /** |
|---|
| 84 | * Hibernate requires a no-arguement constructor (this could be private, I suppose) |
|---|
| 85 | */ |
|---|
| 86 | private LSIDAuthority() { } |
|---|
| 87 | |
|---|
| 88 | /** |
|---|
| 89 | * Construct an LSID authority object. |
|---|
| 90 | * @param String LSID Authority must be valid authority string, or an ID of the form: lsidauth:<validauthoritystring>" |
|---|
| 91 | */ |
|---|
| 92 | public LSIDAuthority(String authstr) throws MalformedLSIDException { |
|---|
| 93 | try { |
|---|
| 94 | authority = authstr.toLowerCase(); |
|---|
| 95 | if (authority.startsWith(AUTHORITY_ID_PREFIX)) |
|---|
| 96 | authority = authority.substring(AUTHORITY_ID_PREFIX.length()); |
|---|
| 97 | } catch (Exception e) { |
|---|
| 98 | throw new MalformedLSIDException(e, "LSID Authority must be valid authority string, or of form: lsidauth:<validauthoritystring>"); |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | /** |
|---|
| 103 | * Convenience constructor, construct an LSID authority object |
|---|
| 104 | * @param LSID use this LSID's authority to construct this object |
|---|
| 105 | */ |
|---|
| 106 | public LSIDAuthority(LSID lsid) throws MalformedLSIDException { |
|---|
| 107 | authority = lsid.getAuthority(); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | /** |
|---|
| 111 | * Returns the authority String |
|---|
| 112 | * @return String the authority String |
|---|
| 113 | */ |
|---|
| 114 | public String getAuthority() { |
|---|
| 115 | return authority; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | /** |
|---|
| 119 | * Returns the authority ID representation of this authority, lsidauth:authstr |
|---|
| 120 | * @return String the ID representation |
|---|
| 121 | */ |
|---|
| 122 | public String getAuthorityID() { |
|---|
| 123 | return AUTHORITY_ID_PREFIX + authority; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | /** |
|---|
| 127 | * Returns the authority String |
|---|
| 128 | * @return String the authority String |
|---|
| 129 | */ |
|---|
| 130 | public String toString() { |
|---|
| 131 | return authority; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | /** |
|---|
| 135 | * Tests equality on the authority string |
|---|
| 136 | * @return |
|---|
| 137 | */ |
|---|
| 138 | public boolean equals(Object o) { |
|---|
| 139 | if (!(o instanceof LSIDAuthority)) |
|---|
| 140 | return false; |
|---|
| 141 | LSIDAuthority auth = (LSIDAuthority)o; |
|---|
| 142 | return o.toString().equals(toString()); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | /** |
|---|
| 146 | * Returns the port of the resolved Authority, invalid until resolved. |
|---|
| 147 | * @return int the port. |
|---|
| 148 | */ |
|---|
| 149 | public int getPort() { |
|---|
| 150 | return port; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | /** |
|---|
| 154 | * Returns the server of the resolved Authority, invalid until resolved. |
|---|
| 155 | * @return String the hostname of the server |
|---|
| 156 | */ |
|---|
| 157 | public String getServer() { |
|---|
| 158 | return server; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | /** |
|---|
| 162 | * Returns the url of the resolved Authority, invalid until resolved. This overrides the |
|---|
| 163 | * server and port properties, and might contain a full path or even a different protocol. |
|---|
| 164 | * @return String |
|---|
| 165 | */ |
|---|
| 166 | @Transient |
|---|
| 167 | public String getUrl() { |
|---|
| 168 | if (url == null) { |
|---|
| 169 | if (server != null && port != -1) |
|---|
| 170 | url = "http://" + getServer() + ":" + getPort() + AUTHORITY_PATH; |
|---|
| 171 | else |
|---|
| 172 | return null; |
|---|
| 173 | } |
|---|
| 174 | return url; |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | /** |
|---|
| 178 | * Sets the port. |
|---|
| 179 | * @param port The port to set |
|---|
| 180 | */ |
|---|
| 181 | public void setPort(int port) { |
|---|
| 182 | this.port = port; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | /** |
|---|
| 186 | * Sets the server. |
|---|
| 187 | * @param server The server to set |
|---|
| 188 | */ |
|---|
| 189 | public void setServer(String server) { |
|---|
| 190 | this.server = server; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | /** |
|---|
| 194 | * Sets the URL to use. This overrides the server and port properties, and might contain a full path or even a |
|---|
| 195 | * different protocol. |
|---|
| 196 | * @param server The server to set |
|---|
| 197 | */ |
|---|
| 198 | public void setUrl(String url) { |
|---|
| 199 | this.url = url; |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | /** |
|---|
| 203 | * Set the wsdl describing the ports available |
|---|
| 204 | * @param LSIDWSDLWrapper the wsdl to set |
|---|
| 205 | */ |
|---|
| 206 | public void setWSDL(Definition wsdl) { |
|---|
| 207 | this.authorityWSDL = wsdl; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | /** |
|---|
| 211 | * get the wsdl describing the ports available |
|---|
| 212 | * @return LSIDWSDLWRapper the wsdl |
|---|
| 213 | */ |
|---|
| 214 | public Definition getWSDL() { |
|---|
| 215 | return this.authorityWSDL; |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | /** |
|---|
| 219 | * @return boolean, whether or not the authority has been resolved. |
|---|
| 220 | */ |
|---|
| 221 | @Transient |
|---|
| 222 | public boolean isResolved() { |
|---|
| 223 | return (getUrl() != null); |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | /** |
|---|
| 227 | * get the url of an authority with the given server and port |
|---|
| 228 | */ |
|---|
| 229 | public String getAuthorityEnpointURL(String server, int port) { |
|---|
| 230 | return AUTHORITY_PROTOCOL + "://" + server + ":" + port + AUTHORITY_PATH; |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | public Map<String,Class<? extends IIdentifiableEntity>> getNamespaces() { |
|---|
| 234 | return namespaces; |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | public void addNamespace(String namespace, Class<? extends IIdentifiableEntity> identifiableClass) { |
|---|
| 238 | this.namespaces.put(namespace, identifiableClass); |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | public void removeNamespace(String namespace) { |
|---|
| 242 | this.namespaces.remove(namespace); |
|---|
| 243 | } |
|---|
| 244 | } |
|---|