protected void write(FeatureCollectionResponse results, OutputStream output, Operation getFeature)
throws ServiceException, IOException, UnsupportedEncodingException {
List featureCollections = results.getFeature();
GetFeatureRequest request = GetFeatureRequest.adapt(getFeature.getParameters()[0]);
// round up the info objects for each feature collection
HashMap<String, Set<ResourceInfo>> ns2metas = new HashMap<String, Set<ResourceInfo>>();
for (int fcIndex = 0; fcIndex < featureCollections.size(); fcIndex++) {
if(request != null) {
List<Query> queries = request.getQueries();
Query queryType = queries.get(fcIndex);
// may have multiple type names in each query, so add them all
for (QName name : queryType.getTypeNames()) {
// get a feature type name from the query
Name featureTypeName = new NameImpl(name.getNamespaceURI(), name.getLocalPart());
ResourceInfo meta = catalog.getResourceByName(featureTypeName, ResourceInfo.class);
if (meta == null) {
throw new WFSException(request, "Could not find feature type " + featureTypeName
+ " in the GeoServer catalog");
}
// add it to the map
Set<ResourceInfo> metas = ns2metas.get(featureTypeName.getNamespaceURI());
if (metas == null) {
metas = new HashSet<ResourceInfo>();
ns2metas.put(featureTypeName.getNamespaceURI(), metas);
}
metas.add(meta);
}
} else {
FeatureType featureType = ((FeatureCollection) featureCollections.get(fcIndex)).getSchema();
//load the metadata for the feature type
String namespaceURI = featureType.getName().getNamespaceURI();
FeatureTypeInfo meta = catalog.getFeatureTypeByName(featureType.getName());
if(meta == null)
throw new WFSException(request, "Could not find feature type " +
featureType.getName() + " in the GeoServer catalog");
//add it to the map
Set metas = (Set) ns2metas.get(namespaceURI);
if (metas == null) {
metas = new HashSet();
ns2metas.put(namespaceURI, metas);
}
metas.add(meta);
}
}
WFSInfo wfs = getInfo();
//set feature bounding parameter
//JD: this is quite bad as its not at all thread-safe, once we remove the configuration
// as being a singleton on trunk/2.0.x this should not be an issue
if ( wfs.isFeatureBounding() ) {
configuration.getProperties().remove( GMLConfiguration.NO_FEATURE_BOUNDS );
}
else {
configuration.getProperties().add( GMLConfiguration.NO_FEATURE_BOUNDS);
}
if (wfs.isCiteCompliant()) {
//cite compliance forces us to forgo srsDimension attribute
configuration.getProperties().add(GMLConfiguration.NO_SRS_DIMENSION);
}
else {
configuration.getProperties().remove(GMLConfiguration.NO_SRS_DIMENSION);
}
//set up the srsname syntax
configuration.setSrsSyntax(wfs.getGML().get(WFSInfo.Version.V_11).getSrsNameStyle().toSrsSyntax());
/*
* Set property encoding featureMemeber as opposed to featureMembers
*
*/
if (wfs.isEncodeFeatureMember()) {
configuration.getProperties().add(GMLConfiguration.ENCODE_FEATURE_MEMBER);
} else {
configuration.getProperties().remove(GMLConfiguration.ENCODE_FEATURE_MEMBER);
}
//declare wfs schema location
Object gft = getFeature.getParameters()[0];
Encoder encoder = createEncoder(configuration, ns2metas, gft);
encoder.setEncoding(Charset.forName( geoServer.getSettings().getCharset() ));
if (wfs.isCanonicalSchemaLocation()) {
encoder.setSchemaLocation(getWfsNamespace(), getCanonicalWfsSchemaLocation());
} else {
encoder.setSchemaLocation(getWfsNamespace(),
buildSchemaURL(request.getBaseURL(), getRelativeWfsSchemaLocation()));
}
//declare application schema namespaces
Map<String, String> params = params("service", "WFS", "version", request.getVersion(),
"request", "DescribeFeatureType");
for (Iterator i = ns2metas.entrySet().iterator(); i.hasNext();) {
Map.Entry entry = (Map.Entry) i.next();
String namespaceURI = (String) entry.getKey();
Set metas = (Set) entry.getValue();
StringBuffer typeNames = new StringBuffer();
for (Iterator m = metas.iterator(); m.hasNext();) {
ResourceInfo ri = (ResourceInfo) m.next();
if(ri instanceof FeatureTypeInfo) {
FeatureTypeInfo meta = (FeatureTypeInfo) ri;
FeatureType featureType = meta.getFeatureType();
Object userSchemaLocation = featureType.getUserData().get("schemaURI");
if (userSchemaLocation != null && userSchemaLocation instanceof Map) {
Map<String, String> schemaURIs = (Map<String, String>) userSchemaLocation;
for (String namespace : schemaURIs.keySet()) {
encoder.setSchemaLocation(namespace, schemaURIs.get(namespace));
}
} else {
typeNames.append(meta.getPrefixedName());
if (m.hasNext()) {
typeNames.append(",");
}
}
}
}
if (typeNames.length() > 0) {
params.put("typeName", typeNames.toString());
// set the made up schema location for types not provided by the user
String schemaLocation = buildURL(request.getBaseURL(), "wfs", params, URLType.SERVICE);
LOGGER.finer("Unable to find user-defined schema location for: " + namespaceURI
+ ". Using a built schema location by default: " + schemaLocation);
encoder.setSchemaLocation(namespaceURI, schemaLocation);
}
}