3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.cdm
.print
;
13 import java
.io
.IOException
;
14 import java
.io
.InputStream
;
16 import java
.net
.URISyntaxException
;
18 import java
.util
.ArrayList
;
19 import java
.util
.Arrays
;
20 import java
.util
.HashMap
;
21 import java
.util
.List
;
23 import java
.util
.UUID
;
25 import org
.apache
.http
.HttpResponse
;
26 import org
.apache
.http
.NameValuePair
;
27 import org
.apache
.http
.message
.BasicNameValuePair
;
28 import org
.apache
.log4j
.Logger
;
29 import org
.jdom
.Document
;
30 import org
.jdom
.Element
;
31 import org
.jdom
.JDOMException
;
33 import eu
.etaxonomy
.cdm
.common
.IProgressMonitor
;
34 import eu
.etaxonomy
.cdm
.common
.UriUtils
;
35 import eu
.etaxonomy
.cdm
.print
.XMLHelper
.EntityType
;
38 * Implementation of an IXMLEntityFactory that is connected to a CDM Community Server on
39 * a remote machine. API call will be executed by accessing the the servers REST API.
42 * @created Apr 6, 2010
45 public class RemoteXMLEntityFactory
extends AbstractXmlEntityFactory
{
46 private static final Logger logger
= Logger
47 .getLogger(RemoteXMLEntityFactory
.class);
49 private URL serviceUrl
;
51 private IProgressMonitor monitor
;
53 private static final List
<NameValuePair
> UNLIMITED_RESULTS
= Arrays
.asList(new NameValuePair
[]{
54 new BasicNameValuePair("start", "0"),
55 new BasicNameValuePair("limit", "-1")
58 private static final String UUID
= "{uuid}";
60 private static final String CLASSIFICATIONS
= "classification";
61 private static final String CLASSIFICATION_CHILD_NODES
= "classification/" + UUID
+ "/childNodes/";
62 private static final String TAXONNODE_CHILD_NODES
= "taxonNode/" + UUID
+ "/childNodes/";
63 private static final String TAXONNODE
= "taxonNode/" + UUID
;
64 private static final String TAXONNODE_TAXON
= TAXONNODE
+ "/taxon";
66 private static final String FEATURETREES
= "featuretrees";
67 private static final String FEATURETREE
= "featuretree/" + UUID
;
68 private static final String FEATURENODE
= "featurenode/" + UUID
;
69 private static final String FEATURENODE_FEATURE
= FEATURENODE
+ "/feature";
71 private static final String NAME_TYPE_DESIGNATIONS
= "name/" + UUID
+ "/typeDesignations";
73 private static final String TAXON_ACCEPTED
= "portal/taxon/" + UUID
;
74 private static final String TAXON_SYNONYMY
= "portal/taxon/" + UUID
+ "/synonymy";
75 private static final String TAXON_DESCRIPTIONS
= "portal/taxon/" + UUID
+ "/descriptions";
82 * Creates new instance of this factory and connects it to the given
83 * CDM Community Stores access point.
85 * Typically, there is no need to instantiate this class.
88 protected RemoteXMLEntityFactory(URL webserviceUrl
, IProgressMonitor monitor
){
89 this.serviceUrl
= webserviceUrl
;
90 this.monitor
= monitor
;
95 * @see eu.etaxonomy.printpublisher.IXMLEntityFactory#getClassifications()
97 public List
<Element
> getClassifications(){
98 Element result
= queryServiceWithParameters(CLASSIFICATIONS
, UNLIMITED_RESULTS
);
99 return processElementList(result
);
104 * @see eu.etaxonomy.printpublisher.IXMLEntityFactory#getChildNodes(org.jdom.Element)
106 public List
<Element
> getChildNodes(Element treeNode
){
107 EntityType entityType
= XMLHelper
.getEntityType(treeNode
);
109 Element result
= null;
111 if(EntityType
.CLASSIFICATION
.equals(entityType
)){
112 result
= queryService(treeNode
, CLASSIFICATION_CHILD_NODES
);
114 else if(EntityType
.TAXON_NODE
.equals(entityType
)){
115 result
= queryService(treeNode
, TAXONNODE_CHILD_NODES
);
118 return processElementList(result
);
123 * @see eu.etaxonomy.printpublisher.IXMLEntityFactory#getTaxonNode(java.util.UUID)
125 public Element
getTaxonNode(UUID taxonNodeUuid
) {
126 return queryService(taxonNodeUuid
, TAXONNODE
);
131 * @see eu.etaxonomy.printpublisher.IXMLEntityFactory#getFeatureTree(java.util.UUID)
133 public Element
getFeatureTree(UUID featureTreeUuid
) {
134 return queryService(featureTreeUuid
, FEATURETREE
);
139 * @see eu.etaxonomy.printpublisher.IXMLEntityFactory#getFeatureTrees()
141 public List
<Element
> getFeatureTrees() {
142 Element result
= queryServiceWithParameters(FEATURETREES
, UNLIMITED_RESULTS
);
143 return processElementList(result
);
146 public Element
getFeatureNode(UUID featureNodeUuid
) {
147 Element result
= queryService(featureNodeUuid
, FEATURENODE
);
151 public Element
getFeatureForFeatureNode(UUID featureNodeUuid
) {
152 Element result
= queryService(featureNodeUuid
, FEATURENODE_FEATURE
);
159 * @see eu.etaxonomy.printpublisher.IXMLEntityFactory#getTaxonFromTaxonNode(org.jdom.Element)
161 public Element
getTaxonForTaxonNode(Element taxonNodeElement
) {
162 return queryService(taxonNodeElement
, TAXONNODE_TAXON
);
168 * @see eu.etaxonomy.printpublisher.IXMLEntityFactory#getAcceptedTaxonElement(org.jdom.Element)
170 public Element
getAcceptedTaxonElement(Element taxonElement
) {
171 Element result
= queryService(taxonElement
, TAXON_ACCEPTED
);
177 * @see eu.etaxonomy.printpublisher.IXMLEntityFactory#getSynonymy(org.jdom.Element)
179 public List
<Element
> getSynonymy(Element taxonElement
) {
180 Element result
= queryService(taxonElement
, TAXON_SYNONYMY
);
182 List
<Element
> elementList
= new ArrayList
<Element
>();
184 for(Object child
: result
.getChildren()){
185 if(child
instanceof Element
){
186 Element childElement
= (Element
) ((Element
)child
).clone();
188 childElement
.detach();
190 elementList
.add(childElement
);
199 * @see eu.etaxonomy.printpublisher.IXMLEntityFactory#getTypeDesignations(org.jdom.Element)
201 public List
<Element
> getTypeDesignations(Element nameElement
) {
202 Element result
= queryService(nameElement
, NAME_TYPE_DESIGNATIONS
);
203 return processElementList(result
);
208 * @see eu.etaxonomy.printpublisher.IXMLEntityFactory#getDescriptions(org.jdom.Element)
210 public Element
getDescriptions(Element taxonElement
) {
211 Element result
= queryService(taxonElement
, TAXON_DESCRIPTIONS
);
216 * Queries the service with the uuid of the given element
222 private Element
queryService(Element element
, String restRequest
){
223 UUID uuid
= XMLHelper
.getUuid(element
);
224 return queryService(uuid
, restRequest
);
228 * Queries the service with the given uuid
234 private Element
queryService(UUID uuid
, String restRequest
){
235 String request
= restRequest
.replace(UUID
, uuid
.toString());
236 return queryServiceWithParameters(request
, null);
243 * @throws URISyntaxException
245 private Element
queryServiceWithParameters(String restRequest
, List
<NameValuePair
> queryParameters
){
248 URI newUri
= UriUtils
.createUri(serviceUrl
, restRequest
, queryParameters
, null);
251 Map
<String
, String
> requestHeaders
= new HashMap
<String
, String
>();
252 requestHeaders
.put("Accept", "application/xml");
253 requestHeaders
.put("Accept-Charset", "UTF-8");
256 HttpResponse response
= UriUtils
.getResponse(newUri
, requestHeaders
);
258 if(UriUtils
.isOk(response
)){
260 logger
.info("Firing request for URI: " + newUri
);
262 // get the content at the resource
263 InputStream content
= UriUtils
.getContent(response
);
265 // build the jdom document
266 Document document
= builder
.build(content
);
269 return document
.getRootElement();
271 monitor
.warning(UriUtils
.getStatus(response
));
272 logger
.error(UriUtils
.getStatus(response
));
275 } catch (IOException e
) {
276 monitor
.warning("No content for request: " + restRequest
, e
);
277 logger
.error("No content for request: " + restRequest
);
278 } catch (JDOMException e
) {
279 monitor
.warning("Error building the document.", e
);
280 logger
.error("Error building the document.", e
);
281 } catch (URISyntaxException e
) {
282 monitor
.warning("Error building URI", e
);
283 logger
.error("Error building URI", e
);
291 * The access point of a CDM Community Server
293 * @return the serviceUrl
295 public URL
getServiceUrl() {
300 * The CDM Community Servers access point
304 public void setServiceUrl(URL serviceUrl
) {
305 this.serviceUrl
= serviceUrl
;