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.geoserver.wfs.response;
006:
007: import net.opengis.wfs.GetFeatureType;
008: import net.opengis.wfs.LockFeatureResponseType;
009: import net.opengis.wfs.LockFeatureType;
010: import net.opengis.wfs.TransactionType;
011:
012: import org.geoserver.ows.Response;
013: import org.geoserver.ows.util.RequestUtils;
014: import org.geoserver.ows.util.ResponseUtils;
015: import org.geoserver.platform.Operation;
016: import org.geoserver.platform.ServiceException;
017: import org.geoserver.wfs.WFS;
018: import org.geoserver.wfs.xml.v1_1_0.WFSConfiguration;
019: import org.geotools.util.Version;
020: import org.geotools.xml.Encoder;
021: import org.opengis.filter.identity.FeatureId;
022: import org.vfny.geoserver.global.Data;
023: import org.xml.sax.SAXException;
024: import java.io.BufferedWriter;
025: import java.io.IOException;
026: import java.io.OutputStream;
027: import java.io.OutputStreamWriter;
028: import java.util.Iterator;
029: import java.util.List;
030:
031: public class LockFeatureTypeResponse extends Response {
032: WFS wfs;
033: Data catalog;
034: WFSConfiguration configuration;
035:
036: public LockFeatureTypeResponse(WFS wfs, Data catalog,
037: WFSConfiguration configuration) {
038: super (LockFeatureResponseType.class);
039: this .wfs = wfs;
040: this .catalog = catalog;
041: this .configuration = configuration;
042: }
043:
044: public String getMimeType(Object value, Operation operation)
045: throws ServiceException {
046: return "text/xml";
047: }
048:
049: public void write(Object value, OutputStream output,
050: Operation operation) throws IOException, ServiceException {
051: LockFeatureResponseType lockResponse = (LockFeatureResponseType) value;
052:
053: if (new Version("1.1.0").equals(operation.getService()
054: .getVersion())) {
055: write1_1(lockResponse, output, operation);
056:
057: return;
058: }
059:
060: String indent = wfs.isVerbose() ? " " : "";
061: BufferedWriter writer = new BufferedWriter(
062: new OutputStreamWriter(output));
063:
064: LockFeatureType lft = (LockFeatureType) operation
065: .getParameters()[0];
066: String proxifiedBaseUrl = RequestUtils.proxifiedBaseURL(lft
067: .getBaseUrl(), wfs.getGeoServer().getProxyBaseUrl());
068:
069: //TODO: get rid of this hardcoding, and make a common utility to get all
070: //these namespace imports, as everyone is using them, and changes should
071: //go through to all the operations.
072: writer.write("<?xml version=\"1.0\" encoding=\""
073: + wfs.getCharSet().name() + "\"?>");
074: writer.write("<WFS_LockFeatureResponse " + "\n");
075: writer.write(indent + "xmlns=\"http://www.opengis.net/wfs\" "
076: + "\n");
077: writer.write(indent
078: + "xmlns:ogc=\"http://www.opengis.net/ogc\" " + "\n");
079:
080: writer
081: .write(indent
082: + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
083: + "\n");
084: writer.write(indent
085: + "xsi:schemaLocation=\"http://www.opengis.net/wfs ");
086: writer.write(ResponseUtils.appendPath(proxifiedBaseUrl,
087: "schemas/wfs/1.0.0/WFS-transaction.xsd"));
088: writer.write("\">" + "\n");
089:
090: writer.write(indent + "<LockId>" + lockResponse.getLockId()
091: + "</LockId>" + "\n");
092:
093: List featuresLocked = null;
094:
095: if (lockResponse.getFeaturesLocked() != null) {
096: featuresLocked = lockResponse.getFeaturesLocked()
097: .getFeatureId();
098: }
099:
100: List featuresNotLocked = null;
101:
102: if (lockResponse.getFeaturesNotLocked() != null) {
103: featuresNotLocked = lockResponse.getFeaturesNotLocked()
104: .getFeatureId();
105: }
106:
107: if ((featuresLocked != null) && !featuresLocked.isEmpty()) {
108: writer.write(indent + "<FeaturesLocked>" + "\n");
109:
110: for (Iterator i = featuresLocked.iterator(); i.hasNext();) {
111: writer.write(indent + indent);
112:
113: FeatureId featureId = (FeatureId) i.next();
114: writer.write("<ogc:FeatureId fid=\"" + featureId
115: + "\"/>" + "\n");
116: }
117:
118: writer.write(indent + "</FeaturesLocked>" + "\n");
119: }
120:
121: if ((featuresNotLocked != null) && !featuresNotLocked.isEmpty()) {
122: writer.write("<FeaturesNotLocked>" + "\n");
123:
124: for (Iterator i = featuresNotLocked.iterator(); i.hasNext();) {
125: writer.write(indent + indent);
126:
127: FeatureId featureId = (FeatureId) i.next();
128: writer.write("<ogc:FeatureId fid=\"" + featureId
129: + "\"/>" + "\n");
130: }
131:
132: writer.write("</FeaturesNotLocked>" + "\n");
133: }
134:
135: writer.write("</WFS_LockFeatureResponse>");
136: writer.flush();
137: }
138:
139: void write1_1(LockFeatureResponseType lockResponse,
140: OutputStream output, Operation operation)
141: throws IOException {
142: Encoder encoder = new Encoder(configuration, configuration
143: .schema());
144:
145: LockFeatureType req = (LockFeatureType) operation
146: .getParameters()[0];
147: String proxifiedBaseUrl = RequestUtils.proxifiedBaseURL(req
148: .getBaseUrl(), wfs.getGeoServer().getProxyBaseUrl());
149:
150: encoder.setSchemaLocation(
151: org.geoserver.wfs.xml.v1_1_0.WFS.NAMESPACE,
152: ResponseUtils.appendPath(proxifiedBaseUrl,
153: "schemas/wfs/1.1.0/wfs.xsd"));
154:
155: try {
156: encoder
157: .encode(
158: lockResponse,
159: org.geoserver.wfs.xml.v1_1_0.WFS.LOCKFEATURERESPONSE,
160: output);
161: } catch (SAXException e) {
162: //SAXException does not sets initCause(). Instead, it holds its own "exception" field.
163: if (e.getException() != null && e.getCause() == null) {
164: e.initCause(e.getException());
165: }
166: throw (IOException) new IOException().initCause(e);
167: }
168:
169: output.flush();
170: }
171: }
|