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.wms.responses.featureInfo;
006:
007: import net.opengis.wfs.FeatureCollectionType;
008: import net.opengis.wfs.GetFeatureType;
009: import net.opengis.wfs.QueryType;
010: import net.opengis.wfs.WfsFactory;
011:
012: import org.geoserver.platform.Operation;
013: import org.geoserver.platform.Service;
014: import org.geoserver.wfs.WFS;
015: import org.geoserver.wfs.WebFeatureService;
016: import org.geoserver.wfs.xml.GML2OutputFormat;
017: import org.geotools.feature.FeatureCollection;
018: import org.geotools.gml2.bindings.GML2EncodingUtils;
019: import org.vfny.geoserver.ServiceException;
020: import org.vfny.geoserver.global.Data;
021: import org.vfny.geoserver.global.FeatureTypeInfo;
022: import org.vfny.geoserver.global.GeoServer;
023: import org.vfny.geoserver.global.WMS;
024: import org.vfny.geoserver.servlets.AbstractService;
025: import org.vfny.geoserver.wms.requests.GetFeatureInfoRequest;
026: import org.vfny.geoserver.wms.servlets.WMService;
027: import java.io.IOException;
028: import java.io.OutputStream;
029: import java.math.BigInteger;
030: import java.net.URI;
031: import java.net.URISyntaxException;
032: import java.util.ArrayList;
033: import java.util.Collections;
034: import java.util.HashMap;
035: import java.util.Iterator;
036: import java.util.List;
037:
038: /**
039: * A GetFeatureInfo response handler specialized in producing GML data for a
040: * GetFeatureInfo request.
041: *
042: * <p>
043: * This class does not deals directly with GML encoding. Instead, it works by
044: * taking the FeatureResults produced in <code>execute()</code> and constructs
045: * a <code>GetFeaturesResult</code> wich is passed to a
046: * <code>GML2FeatureResponseDelegate</code>, as if it where the result of a
047: * GetFeature WFS request.
048: * </p>
049: *
050: * @author Gabriel Roldan, Axios Engineering
051: */
052: public class GmlFeatureInfoResponse extends AbstractFeatureInfoResponse {
053: /**
054: * The MIME type of the format this response produces:
055: * <code>"application/vnd.ogc.gml"</code>
056: */
057: private static final String FORMAT = "application/vnd.ogc.gml";
058:
059: /**
060: * Default constructor, sets up the supported output format string.
061: */
062: public GmlFeatureInfoResponse() {
063: super .supportedFormats = Collections.singletonList(FORMAT);
064: }
065:
066: /**
067: * Returns any extra headers that this service might want to set in the HTTP
068: * response object.
069: *
070: * @see org.vfny.geoserver.Response#getResponseHeaders()
071: */
072: public HashMap getResponseHeaders() {
073: return new HashMap();
074: }
075:
076: /**
077: * Takes the <code>FeatureResult</code>s generated by the
078: * <code>execute</code> method in the superclass and constructs a
079: * <code>GetFeaturesResult</code> wich is passed to a
080: * <code>GML2FeatureResponseDelegate</code>.
081: *
082: * @param out
083: * DOCUMENT ME!
084: *
085: * @throws ServiceException
086: * DOCUMENT ME!
087: * @throws IOException
088: * DOCUMENT ME!
089: */
090: public void writeTo(OutputStream out) throws ServiceException,
091: IOException {
092: GetFeatureInfoRequest fInfoReq = (GetFeatureInfoRequest) getRequest();
093: WMS wms = (WMS) fInfoReq.getServiceRef().getServiceRef();
094: WFS wfs = wms.getWFS();
095: GeoServer gs = wms.getGeoServer();
096:
097: Data catalog = fInfoReq.getServiceRef().getCatalog();
098:
099: //the 'response' object we'll pass to our OutputFormat
100: FeatureCollectionType features = WfsFactory.eINSTANCE
101: .createFeatureCollectionType();
102:
103: //the 'request' object we'll pass to our OutputFormat
104: GetFeatureType gfreq = WfsFactory.eINSTANCE
105: .createGetFeatureType();
106: gfreq.setBaseUrl(fInfoReq.getBaseUrl());
107:
108: for (Iterator i = results.iterator(); i.hasNext();) {
109: FeatureCollection fc = (FeatureCollection) i.next();
110: features.getFeature().add(fc);
111:
112: QueryType qt = WfsFactory.eINSTANCE.createQueryType();
113: String crs = GML2EncodingUtils.epsgCode(fc.getSchema()
114: .getDefaultGeometry().getCoordinateSystem());
115: if (crs != null) {
116: final String srsName = "EPSG:" + crs;
117: try {
118: qt.setSrsName(new URI(srsName));
119: } catch (URISyntaxException e) {
120: throw new ServiceException(
121: "Unable to determite coordinate system for featureType "
122: + fc.getSchema().getTypeName()
123: + ". Schema told us '" + srsName
124: + "'", e);
125: }
126: }
127: gfreq.getQuery().add(qt);
128:
129: }
130:
131: //this is a dummy wrapper around our 'request' object so that the new Dispatcher will accept it.
132: Service serviceDesc = new Service("wms", null, null);
133: Operation opDescriptor = new Operation("", serviceDesc, null,
134: new Object[] { gfreq });
135:
136: GML2OutputFormat format = new GML2OutputFormat(wfs, gs, catalog);
137: format.write(features, out, opDescriptor);
138: }
139:
140: public String getContentDisposition() {
141: // TODO Auto-generated method stub
142: return null;
143: }
144: }
|