001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005: package org.vfny.geoserver.wcs.responses;
006:
007: import org.geoserver.ows.util.RequestUtils;
008: import org.geoserver.ows.util.ResponseUtils;
009: import org.geoserver.platform.GeoServerExtensions;
010: import org.geotools.geometry.GeneralEnvelope;
011: import org.geotools.xml.transform.TransformerBase;
012: import org.geotools.xml.transform.Translator;
013: import org.springframework.context.ApplicationContext;
014: import org.vfny.geoserver.global.CoverageInfo;
015: import org.vfny.geoserver.global.CoverageInfoLabelComparator;
016: import org.vfny.geoserver.global.GeoServer;
017: import org.vfny.geoserver.global.MetaDataLink;
018: import org.vfny.geoserver.global.Service;
019: import org.vfny.geoserver.global.WCS;
020: import org.vfny.geoserver.util.requests.CapabilitiesRequest;
021: import org.vfny.geoserver.wcs.requests.WCSRequest;
022: import org.xml.sax.ContentHandler;
023: import org.xml.sax.SAXException;
024: import org.xml.sax.helpers.AttributesImpl;
025:
026: import java.util.ArrayList;
027: import java.util.Collections;
028: import java.util.Iterator;
029: import java.util.List;
030: import java.util.Set;
031: import java.util.logging.Logger;
032:
033: import javax.xml.transform.OutputKeys;
034: import javax.xml.transform.Transformer;
035: import javax.xml.transform.TransformerException;
036:
037: /**
038: * DOCUMENT ME!
039: *
040: * @author $Author: Alessio Fabiani (alessio.fabiani@gmail.com) $ (last
041: * modification)
042: * @author $Author: Simone Giannecchini (simboss1@gmail.com) $ (last
043: * modification)
044: */
045: public class WCSCapsTransformer extends TransformerBase {
046: /** DOCUMENT ME! */
047: private static final Logger LOGGER = org.geotools.util.logging.Logging
048: .getLogger(WCSCapsTransformer.class.getPackage().getName());
049: protected static final String WCS_URI = "http://www.opengis.net/wcs";
050:
051: /** DOCUMENT ME! */
052: protected static final String WFS_URI = "http://www.opengis.net/wcs";
053:
054: /** DOCUMENT ME! */
055: protected static final String CUR_VERSION = "1.0.0";
056:
057: /** DOCUMENT ME! */
058: protected static final String XSI_PREFIX = "xsi";
059:
060: /** DOCUMENT ME! */
061: protected static final String XSI_URI = "http://www.w3.org/2001/XMLSchema-instance";
062:
063: /** DOCUMENT ME! */
064: private String baseUrl;
065:
066: private ApplicationContext applicationContext;
067:
068: /**
069: * DOCUMENT ME!
070: *
071: * @uml.property name="request"
072: * @uml.associationEnd multiplicity="(0 1)"
073: */
074: protected WCSRequest request;
075:
076: /**
077: * Creates a new WFSCapsTransformer object.
078: */
079: public WCSCapsTransformer(String baseUrl,
080: ApplicationContext applicationContext) {
081: super ();
082: if (baseUrl == null) {
083: throw new NullPointerException();
084: }
085:
086: this .baseUrl = baseUrl;
087: this .setNamespaceDeclarationEnabled(false);
088: this .applicationContext = applicationContext;
089: }
090:
091: /**
092: * DOCUMENT ME!
093: *
094: * @param handler
095: * DOCUMENT ME!
096: *
097: * @return DOCUMENT ME!
098: */
099: public Translator createTranslator(ContentHandler handler) {
100: return new WCSCapsTranslator(handler, applicationContext);
101: }
102:
103: /**
104: * DOCUMENT ME!
105: *
106: * @return a Transformer propoerly configured to produce DescribeLayer
107: * responses.
108: *
109: * @throws TransformerException
110: * if it is thrown by <code>super.createTransformer()</code>
111: */
112: public Transformer createTransformer() throws TransformerException {
113: Transformer transformer = super .createTransformer();
114: GeoServer gs = (GeoServer) GeoServerExtensions.extensions(
115: GeoServer.class, applicationContext).get(0);
116: String dtdUrl = RequestUtils.proxifiedBaseURL(this .baseUrl, gs
117: .getProxyBaseUrl())
118: + "schemas/wcs/1.0.0/wcsCapabilities.xsd"; // DJB: fixed
119: // this to
120: // point to correct
121: // location
122:
123: //JD: disabling doctype because it throws off the cite test parser.
124: // I do not beleive that a doctype def is necessary anyways since
125: // there is a xml schema for wcs 1.0, and plus, this doctype url points tp
126: // an xml schema, which is just plain wrong
127: //transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dtdUrl);
128:
129: return transformer;
130: }
131:
132: /**
133: * DOCUMENT ME!
134: *
135: * @author Gabriel Roldan, Axios Engineering
136: * @version $Id
137: */
138: private static class WCSCapsTranslator extends TranslatorSupport {
139: /**
140: * DOCUMENT ME!
141: *
142: * @uml.property name="request"
143: * @uml.associationEnd multiplicity="(0 1)"
144: */
145: private CapabilitiesRequest request;
146:
147: private ApplicationContext applicationContext;
148:
149: /**
150: * Creates a new WFSCapsTranslator object.
151: *
152: * @param handler
153: * DOCUMENT ME!
154: */
155: public WCSCapsTranslator(ContentHandler handler,
156: ApplicationContext applicationContext) {
157: super (handler, null, null);
158: this .applicationContext = applicationContext;
159: }
160:
161: /**
162: * Encode the object.
163: *
164: * @param o
165: * The Object to encode.
166: *
167: * @throws IllegalArgumentException
168: * if the Object is not encodeable.
169: */
170: public void encode(Object o) throws IllegalArgumentException {
171: if (!(o instanceof CapabilitiesRequest)) {
172: throw new IllegalArgumentException(new StringBuffer(
173: "Not a CapabilitiesRequest: ").append(o)
174: .toString());
175: }
176:
177: this .request = (CapabilitiesRequest) o;
178: final WCS wcs = (WCS) request.getServiceRef()
179: .getServiceRef();
180:
181: final AttributesImpl attributes = new AttributesImpl();
182: attributes.addAttribute("", "version", "version", "",
183: CUR_VERSION);
184: attributes.addAttribute("", "xmlns", "xmlns", "", WCS_URI);
185:
186: attributes.addAttribute("", "xmlns:xlink", "xmlns:xlink",
187: "", "http://www.w3.org/1999/xlink");
188: attributes.addAttribute("", "xmlns:ogc", "xmlns:ogc", "",
189: "http://www.opengis.net/ogc");
190: attributes.addAttribute("", "xmlns:gml", "xmlns:gml", "",
191: "http://www.opengis.net/gml");
192:
193: final String prefixDef = new StringBuffer("xmlns:").append(
194: XSI_PREFIX).toString();
195: attributes.addAttribute("", prefixDef, prefixDef, "",
196: XSI_URI);
197:
198: final String locationAtt = new StringBuffer(XSI_PREFIX)
199: .append(":schemaLocation").toString();
200:
201: final String locationDef = WCS_URI
202: + " "
203: + RequestUtils.proxifiedBaseURL(request
204: .getBaseUrl(), wcs.getGeoServer()
205: .getProxyBaseUrl())
206: + "schemas/wcs/1.0.0/wcsCapabilities.xsd";
207:
208: attributes.addAttribute("", locationAtt, locationAtt, "",
209: locationDef);
210: attributes.addAttribute("", "updateSequence",
211: "updateSequence", "", wcs.getGeoServer()
212: .getUpdateSequence()
213: + "");
214: start("WCS_Capabilities", attributes);
215:
216: handleService();
217: handleCapabilities();
218:
219: end("WCS_Capabilities");
220: }
221:
222: /**
223: * Handles the service section of the capabilities document.
224: *
225: * @param config
226: * The OGC service to transform.
227: *
228: * @throws SAXException
229: * For any errors.
230: */
231: private void handleService() {
232: final WCS wcs = (WCS) request.getServiceRef()
233: .getServiceRef();
234: AttributesImpl attributes = new AttributesImpl();
235: attributes.addAttribute("", "version", "version", "",
236: CUR_VERSION);
237: start("Service", attributes);
238: handleMetadataLink(wcs.getMetadataLink());
239: element("description", wcs.getAbstract());
240: element("name", wcs.getName());
241: element("label", wcs.getTitle());
242: handleKeywords(wcs.getKeywords());
243: handleContact(wcs);
244:
245: String fees = wcs.getFees();
246:
247: if ((fees == null) || "".equals(fees)) {
248: fees = "NONE";
249: }
250:
251: element("fees", fees);
252:
253: String accessConstraints = wcs.getAccessConstraints();
254:
255: if ((accessConstraints == null)
256: || "".equals(accessConstraints)) {
257: accessConstraints = "NONE";
258: }
259:
260: element("accessConstraints", accessConstraints);
261: end("Service");
262: }
263:
264: /**
265: * DOCUMENT ME!
266: *
267: * @param serviceConfig
268: * DOCUMENT ME!
269: *
270: * @throws SAXException
271: * DOCUMENT ME!
272: */
273: private void handleCapabilities() {
274: final WCS wcs = (WCS) request.getServiceRef()
275: .getServiceRef();
276: start("Capability");
277: handleRequest(wcs);
278: handleExceptions(wcs);
279: handleVendorSpecifics(wcs);
280: end("Capability");
281:
282: handleContentMetadata(wcs);
283: }
284:
285: /**
286: * Handles the request portion of the document, printing out the
287: * capabilities and where to bind to them.
288: *
289: * @param config
290: * The global wms.
291: *
292: * @throws SAXException
293: * For any problems.
294: */
295: private void handleRequest(WCS config) {
296: start("Request");
297: handleCapability(config, "GetCapabilities");
298: handleCapability(config, "DescribeCoverage");
299: handleCapability(config, "GetCoverage");
300: end("Request");
301: }
302:
303: private void handleCapability(WCS config, String capabilityName) {
304: AttributesImpl attributes = new AttributesImpl();
305: start(capabilityName);
306:
307: start("DCPType");
308: start("HTTP");
309:
310: String url = "";
311: String baseUrl = RequestUtils.proxifiedBaseURL(request
312: .getBaseUrl(), request.getServiceRef()
313: .getGeoServer().getProxyBaseUrl());
314: baseUrl = ResponseUtils.appendPath(baseUrl, "wcs");
315:
316: //ensure ends in "?" or "&"
317: baseUrl = ResponseUtils.appendQueryString(baseUrl, "");
318:
319: if (request.isDispatchedRequest()) {
320: url = baseUrl;
321: } else {
322: url = new StringBuffer(baseUrl).append("/").append(
323: capabilityName).append("?").toString();
324: }
325:
326: attributes.addAttribute("", "xlink:href", "xlink:href", "",
327: url);
328:
329: start("Get");
330: start("OnlineResource", attributes);
331: end("OnlineResource");
332: end("Get");
333: end("HTTP");
334: end("DCPType");
335:
336: attributes = new AttributesImpl();
337:
338: if (request.isDispatchedRequest()) {
339: url = baseUrl;
340: } else {
341: url = new StringBuffer(baseUrl).append("/").append(
342: capabilityName).toString();
343: }
344:
345: attributes.addAttribute("", "xlink:href", "xlink:href", "",
346: url);
347:
348: start("DCPType");
349: start("HTTP");
350: start("Post");
351: start("OnlineResource", attributes);
352: end("OnlineResource");
353: end("Post");
354: end("HTTP");
355: end("DCPType");
356: end(capabilityName);
357: }
358:
359: /**
360: * DOCUMENT ME!
361: *
362: * @param kwords
363: * DOCUMENT ME!
364: *
365: * @throws SAXException
366: * DOCUMENT ME!
367: */
368: private void handleKeywords(List kwords) {
369: start("keywords");
370:
371: if (kwords != null) {
372: for (Iterator it = kwords.iterator(); it.hasNext();) {
373: element("keyword", it.next().toString());
374: }
375: }
376:
377: end("keywords");
378: }
379:
380: /**
381: * Handles contacts.
382: *
383: * @param config
384: * the service.
385: */
386: private void handleContact(Service config) {
387: String tmp = "";
388:
389: if (((config.getGeoServer().getContactPerson() != null) && (config
390: .getGeoServer().getContactPerson() != ""))
391: || ((config.getGeoServer().getContactOrganization() != null) && (config
392: .getGeoServer().getContactOrganization() != ""))) {
393: start("responsibleParty");
394:
395: tmp = config.getGeoServer().getContactPerson();
396:
397: if ((tmp != null) && (tmp != "")) {
398: element("individualName", tmp);
399:
400: tmp = config.getGeoServer()
401: .getContactOrganization();
402:
403: if ((tmp != null) && (tmp != "")) {
404: element("organisationName", tmp);
405: }
406: } else {
407: tmp = config.getGeoServer()
408: .getContactOrganization();
409:
410: if ((tmp != null) && (tmp != "")) {
411: element("organisationName", tmp);
412: }
413: }
414:
415: tmp = config.getGeoServer().getContactPosition();
416:
417: if ((tmp != null) && (tmp != "")) {
418: element("positionName", tmp);
419: }
420:
421: start("contactInfo");
422:
423: start("phone");
424: tmp = config.getGeoServer().getContactVoice();
425:
426: if ((tmp != null) && (tmp != "")) {
427: element("voice", tmp);
428: }
429:
430: tmp = config.getGeoServer().getContactFacsimile();
431:
432: if ((tmp != null) && (tmp != "")) {
433: element("facsimile", tmp);
434: }
435:
436: end("phone");
437:
438: start("address");
439: tmp = config.getGeoServer().getAddressType();
440:
441: if ((tmp != null) && (tmp != "")) {
442: String addr = "";
443: addr = config.getGeoServer().getAddress();
444:
445: if ((addr != null) && (addr != "")) {
446: element("deliveryPoint", tmp + " " + addr);
447: }
448: } else {
449: tmp = config.getGeoServer().getAddress();
450:
451: if ((tmp != null) && (tmp != "")) {
452: element("deliveryPoint", tmp);
453: }
454: }
455:
456: tmp = config.getGeoServer().getAddressCity();
457:
458: if ((tmp != null) && (tmp != "")) {
459: element("city", tmp);
460: }
461:
462: tmp = config.getGeoServer().getAddressState();
463:
464: if ((tmp != null) && (tmp != "")) {
465: element("administrativeArea", tmp);
466: }
467:
468: tmp = config.getGeoServer().getAddressPostalCode();
469:
470: if ((tmp != null) && (tmp != "")) {
471: element("postalCode", tmp);
472: }
473:
474: tmp = config.getGeoServer().getAddressCountry();
475:
476: if ((tmp != null) && (tmp != "")) {
477: element("country", tmp);
478: }
479:
480: tmp = config.getGeoServer().getContactEmail();
481:
482: if ((tmp != null) && (tmp != "")) {
483: element("electronicMailAddress", tmp);
484: }
485:
486: end("address");
487:
488: tmp = config.getGeoServer().getOnlineResource();
489:
490: if ((tmp != null) && (tmp != "")) {
491: AttributesImpl attributes = new AttributesImpl();
492: attributes.addAttribute("", "xlink:href",
493: "xlink:href", "", tmp);
494: start("onlineResource", attributes);
495: end("onlineResource");
496: }
497:
498: end("contactInfo");
499:
500: end("responsibleParty");
501: }
502: }
503:
504: /**
505: * Handles the printing of the exceptions information, prints the
506: * formats that GeoServer can return exceptions in.
507: *
508: * @param config
509: * The wms service global config.
510: *
511: * @throws SAXException
512: * For any problems.
513: */
514: private void handleExceptions(WCS config) {
515: start("Exception");
516:
517: final String[] formats = config.getExceptionFormats();
518: final int length = formats.length;
519:
520: for (int i = 0; i < length; i++) {
521: element("Format", formats[i]);
522: }
523:
524: end("Exception");
525: }
526:
527: /**
528: * Handles the vendor specific capabilities. Right now there are none,
529: * so we do nothing.
530: *
531: * @param config
532: * The global config that may contain vendor specifics.
533: *
534: * @throws SAXException
535: * For any problems.
536: */
537: private void handleVendorSpecifics(WCS config) {
538: }
539:
540: private void handleEnvelope(GeneralEnvelope envelope) {
541: AttributesImpl attributes = new AttributesImpl();
542:
543: attributes.addAttribute("", "srsName", "srsName", "", /*"urn:ogc:def:crs:OGC:1.3:CRS84"*/
544: "WGS84(DD)");
545: start("lonLatEnvelope", attributes);
546: element("gml:pos",
547: new StringBuffer(Double.toString(envelope
548: .getLowerCorner().getOrdinate(0))).append(
549: " ").append(
550: envelope.getLowerCorner().getOrdinate(1))
551: .toString());
552: element("gml:pos",
553: new StringBuffer(Double.toString(envelope
554: .getUpperCorner().getOrdinate(0))).append(
555: " ").append(
556: envelope.getUpperCorner().getOrdinate(1))
557: .toString());
558: end("lonLatEnvelope");
559: }
560:
561: /**
562: * DOCUMENT ME!
563: *
564: * @param metadataLink
565: * DOCUMENT ME!
566: *
567: * @throws SAXException
568: * DOCUMENT ME!
569: */
570: private void handleMetadataLink(MetaDataLink mdl) {
571: if (mdl != null) {
572: AttributesImpl attributes = new AttributesImpl();
573:
574: if ((mdl.getAbout() != null) && (mdl.getAbout() != "")) {
575: attributes.addAttribute("", "about", "about", "",
576: mdl.getAbout());
577: }
578:
579: // if( mdl.getType() != null && mdl.getType() != "" ) {
580: // attributes.addAttribute("", "type", "type", "",
581: // mdl.getType());
582: // }
583: if ((mdl.getMetadataType() != null)
584: && (mdl.getMetadataType() != "")) {
585: attributes.addAttribute("", "metadataType",
586: "metadataType", "", mdl.getMetadataType());
587: }
588:
589: if (attributes.getLength() > 0) {
590: start("metadataLink", attributes);
591: // characters(mdl.getContent());
592: end("metadataLink");
593: }
594: }
595: }
596:
597: private void handleContentMetadata(WCS config) {
598: AttributesImpl attributes = new AttributesImpl();
599: attributes.addAttribute("", "version", "version", "",
600: CUR_VERSION);
601:
602: start("ContentMetadata", attributes);
603:
604: List coverages = new ArrayList(config.getData()
605: .getCoverageInfos().values());
606: Collections.sort(coverages,
607: new CoverageInfoLabelComparator());
608: for (Iterator i = coverages.iterator(); i.hasNext();) {
609: handleCoverageOfferingBrief(config, (CoverageInfo) i
610: .next());
611: }
612:
613: end("ContentMetadata");
614: }
615:
616: private void handleCoverageOfferingBrief(WCS config,
617: CoverageInfo cv) {
618: if (cv.isEnabled()) {
619: start("CoverageOfferingBrief");
620:
621: String tmp;
622:
623: handleMetadataLink(cv.getMetadataLink());
624: tmp = cv.getDescription();
625:
626: if ((tmp != null) && (tmp != "")) {
627: element("description", tmp);
628: }
629:
630: tmp = cv.getName();
631:
632: if ((tmp != null) && (tmp != "")) {
633: element("name", tmp);
634: }
635:
636: tmp = cv.getLabel();
637:
638: if ((tmp != null) && (tmp != "")) {
639: element("label", tmp);
640: }
641:
642: handleEnvelope(cv.getWGS84LonLatEnvelope());
643: handleKeywords(cv.getKeywords());
644:
645: end("CoverageOfferingBrief");
646: }
647: }
648: }
649: }
|