895378d9fb118f4bd7da30909b91110c19cbc6e7
[cdmlib.git] / cdmlib-print / src / main / java / eu / etaxonomy / cdm / print / RemoteXMLEntityFactory.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.cdm.print;
12
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.net.URI;
16 import java.net.URISyntaxException;
17 import java.net.URL;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.UUID;
24
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;
32
33 import eu.etaxonomy.cdm.common.IProgressMonitor;
34 import eu.etaxonomy.cdm.common.UriUtils;
35 import eu.etaxonomy.cdm.print.XMLHelper.EntityType;
36
37 /**
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.
40 *
41 * @author n.hoffmann
42 * @created Apr 6, 2010
43 * @version 1.0
44 */
45 public class RemoteXMLEntityFactory extends AbstractXmlEntityFactory{
46 private static final Logger logger = Logger
47 .getLogger(RemoteXMLEntityFactory.class);
48
49 private URL serviceUrl;
50
51 private IProgressMonitor monitor;
52
53 private static final List<NameValuePair> UNLIMITED_RESULTS = Arrays.asList(new NameValuePair[]{
54 new BasicNameValuePair("start", "0"),
55 new BasicNameValuePair("limit", "-1")
56 });
57
58 private static final String UUID = "{uuid}";
59
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";
65
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";
70
71 private static final String NAME_TYPE_DESIGNATIONS = "name/" + UUID + "/typeDesignations";
72
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";
76
77
78
79
80
81 /**
82 * Creates new instance of this factory and connects it to the given
83 * CDM Community Stores access point.
84 *
85 * Typically, there is no need to instantiate this class.
86 * @param monitor
87 */
88 protected RemoteXMLEntityFactory(URL webserviceUrl, IProgressMonitor monitor){
89 this.serviceUrl = webserviceUrl;
90 this.monitor = monitor;
91 }
92
93 /*
94 * (non-Javadoc)
95 * @see eu.etaxonomy.printpublisher.IXMLEntityFactory#getClassifications()
96 */
97 public List<Element> getClassifications(){
98 Element result = queryServiceWithParameters(CLASSIFICATIONS, UNLIMITED_RESULTS);
99 return processElementList(result);
100 }
101
102 /*
103 * (non-Javadoc)
104 * @see eu.etaxonomy.printpublisher.IXMLEntityFactory#getChildNodes(org.jdom.Element)
105 */
106 public List<Element> getChildNodes(Element treeNode){
107 EntityType entityType = XMLHelper.getEntityType(treeNode);
108
109 Element result = null;
110
111 if(EntityType.CLASSIFICATION.equals(entityType)){
112 result = queryService(treeNode, CLASSIFICATION_CHILD_NODES);
113 }
114 else if(EntityType.TAXON_NODE.equals(entityType)){
115 result = queryService(treeNode, TAXONNODE_CHILD_NODES);
116 }
117
118 return processElementList(result);
119 }
120
121 /*
122 * (non-Javadoc)
123 * @see eu.etaxonomy.printpublisher.IXMLEntityFactory#getTaxonNode(java.util.UUID)
124 */
125 public Element getTaxonNode(UUID taxonNodeUuid) {
126 return queryService(taxonNodeUuid, TAXONNODE);
127 }
128
129 /*
130 * (non-Javadoc)
131 * @see eu.etaxonomy.printpublisher.IXMLEntityFactory#getFeatureTree(java.util.UUID)
132 */
133 public Element getFeatureTree(UUID featureTreeUuid) {
134 return queryService(featureTreeUuid, FEATURETREE);
135 }
136
137 /*
138 * (non-Javadoc)
139 * @see eu.etaxonomy.printpublisher.IXMLEntityFactory#getFeatureTrees()
140 */
141 public List<Element> getFeatureTrees() {
142 Element result = queryServiceWithParameters(FEATURETREES, UNLIMITED_RESULTS);
143 return processElementList(result);
144 }
145
146 public Element getFeatureNode(UUID featureNodeUuid) {
147 Element result = queryService(featureNodeUuid, FEATURENODE);
148 return result;
149 }
150
151 public Element getFeatureForFeatureNode(UUID featureNodeUuid) {
152 Element result = queryService(featureNodeUuid, FEATURENODE_FEATURE);
153 return result;
154 }
155
156
157 /*
158 * (non-Javadoc)
159 * @see eu.etaxonomy.printpublisher.IXMLEntityFactory#getTaxonFromTaxonNode(org.jdom.Element)
160 */
161 public Element getTaxonForTaxonNode(Element taxonNodeElement) {
162 return queryService(taxonNodeElement, TAXONNODE_TAXON);
163 }
164
165
166 /*
167 * (non-Javadoc)
168 * @see eu.etaxonomy.printpublisher.IXMLEntityFactory#getAcceptedTaxonElement(org.jdom.Element)
169 */
170 public Element getAcceptedTaxonElement(Element taxonElement) {
171 Element result = queryService(taxonElement, TAXON_ACCEPTED);
172 return result;
173 }
174
175 /*
176 * (non-Javadoc)
177 * @see eu.etaxonomy.printpublisher.IXMLEntityFactory#getSynonymy(org.jdom.Element)
178 */
179 public List<Element> getSynonymy(Element taxonElement) {
180 Element result = queryService(taxonElement, TAXON_SYNONYMY);
181
182 List<Element> elementList = new ArrayList<Element>();
183
184 for(Object child : result.getChildren()){
185 if(child instanceof Element){
186 Element childElement = (Element) ((Element)child).clone();
187
188 childElement.detach();
189
190 elementList.add(childElement);
191 }
192 }
193
194 return elementList;
195 }
196
197 /*
198 * (non-Javadoc)
199 * @see eu.etaxonomy.printpublisher.IXMLEntityFactory#getTypeDesignations(org.jdom.Element)
200 */
201 public List<Element> getTypeDesignations(Element nameElement) {
202 Element result = queryService(nameElement, NAME_TYPE_DESIGNATIONS);
203 return processElementList(result);
204 }
205
206 /*
207 * (non-Javadoc)
208 * @see eu.etaxonomy.printpublisher.IXMLEntityFactory#getDescriptions(org.jdom.Element)
209 */
210 public Element getDescriptions(Element taxonElement) {
211 Element result = queryService(taxonElement, TAXON_DESCRIPTIONS);
212 return result;
213 }
214
215 /**
216 * Queries the service with the uuid of the given element
217 *
218 * @param element
219 * @param restRequest
220 * @return
221 */
222 private Element queryService(Element element, String restRequest){
223 UUID uuid = XMLHelper.getUuid(element);
224 return queryService(uuid, restRequest);
225 }
226
227 /**
228 * Queries the service with the given uuid
229 *
230 * @param uuid
231 * @param restRequest
232 * @return
233 */
234 private Element queryService(UUID uuid, String restRequest){
235 String request = restRequest.replace(UUID, uuid.toString());
236 return queryServiceWithParameters(request, null);
237 }
238
239 /**
240 *
241 * @param restRequest
242 * @return
243 * @throws URISyntaxException
244 */
245 private Element queryServiceWithParameters(String restRequest, List<NameValuePair> queryParameters){
246
247 try {
248 URI newUri = UriUtils.createUri(serviceUrl, restRequest, queryParameters, null);
249
250
251 Map<String, String> requestHeaders = new HashMap<String, String>();
252 requestHeaders.put("Accept", "application/xml");
253 requestHeaders.put("Accept-Charset", "UTF-8");
254
255
256 HttpResponse response = UriUtils.getResponse(newUri, requestHeaders);
257
258 if(UriUtils.isOk(response)){
259
260 logger.info("Firing request for URI: " + newUri);
261
262 // get the content at the resource
263 InputStream content = UriUtils.getContent(response);
264
265 // build the jdom document
266 Document document = builder.build(content);
267
268
269 return document.getRootElement();
270 }else{
271 monitor.warning(UriUtils.getStatus(response));
272 logger.error(UriUtils.getStatus(response));
273 }
274
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);
284 }
285
286 // error
287 return null;
288 }
289
290 /**
291 * The access point of a CDM Community Server
292 *
293 * @return the serviceUrl
294 */
295 public URL getServiceUrl() {
296 return serviceUrl;
297 }
298
299 /**
300 * The CDM Community Servers access point
301 *
302 * @param serviceUrl
303 */
304 public void setServiceUrl(URL serviceUrl) {
305 this.serviceUrl = serviceUrl;
306 }
307 }