PageRenderTime 99ms CodeModel.GetById 3ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/geotools-9.2/modules/unsupported/wfs-ng/src/test/java/org/geotools/data/wfs/integration/IntegrationTestWFSClient.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 371 lines | 299 code | 68 blank | 4 comment | 33 complexity | 97ae6bd9f45f84a66b5f2e90b189070b MD5 | raw file
  1. package org.geotools.data.wfs.integration;
  2. import java.io.IOException;
  3. import java.math.BigInteger;
  4. import java.net.URL;
  5. import java.nio.charset.Charset;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import java.util.Iterator;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.concurrent.atomic.AtomicInteger;
  12. import javax.xml.namespace.QName;
  13. import net.opengis.wfs.InsertedFeatureType;
  14. import net.opengis.wfs.TransactionResponseType;
  15. import net.opengis.wfs.WfsFactory;
  16. import org.geotools.data.DataUtilities;
  17. import org.geotools.data.Diff;
  18. import org.geotools.data.DiffFeatureReader;
  19. import org.geotools.data.FeatureReader;
  20. import org.geotools.data.ows.HTTPResponse;
  21. import org.geotools.data.ows.Request;
  22. import org.geotools.data.ows.Response;
  23. import org.geotools.data.ows.SimpleHttpClient;
  24. import org.geotools.data.wfs.impl.TestHttpResponse;
  25. import org.geotools.data.wfs.internal.AbstractWFSStrategy;
  26. import org.geotools.data.wfs.internal.DescribeFeatureTypeRequest;
  27. import org.geotools.data.wfs.internal.DescribeFeatureTypeResponse;
  28. import org.geotools.data.wfs.internal.GetCapabilitiesRequest;
  29. import org.geotools.data.wfs.internal.GetCapabilitiesResponse;
  30. import org.geotools.data.wfs.internal.GetFeatureParser;
  31. import org.geotools.data.wfs.internal.GetFeatureRequest;
  32. import org.geotools.data.wfs.internal.GetFeatureResponse;
  33. import org.geotools.data.wfs.internal.TransactionRequest;
  34. import org.geotools.data.wfs.internal.TransactionRequest.Delete;
  35. import org.geotools.data.wfs.internal.TransactionRequest.Insert;
  36. import org.geotools.data.wfs.internal.TransactionRequest.TransactionElement;
  37. import org.geotools.data.wfs.internal.TransactionRequest.Update;
  38. import org.geotools.data.wfs.internal.WFSClient;
  39. import org.geotools.data.wfs.internal.WFSConfig;
  40. import org.geotools.data.wfs.internal.WFSResponse;
  41. import org.geotools.data.wfs.internal.WFSStrategy;
  42. import org.geotools.factory.CommonFactoryFinder;
  43. import org.geotools.ows.ServiceException;
  44. import org.geotools.wfs.WFS;
  45. import org.geotools.xml.Configuration;
  46. import org.geotools.xml.Encoder;
  47. import org.opengis.feature.simple.SimpleFeature;
  48. import org.opengis.feature.simple.SimpleFeatureType;
  49. import org.opengis.feature.type.FeatureType;
  50. import org.opengis.filter.Filter;
  51. import org.opengis.filter.FilterFactory2;
  52. import org.opengis.filter.identity.FeatureId;
  53. import com.vividsolutions.jts.geom.GeometryFactory;
  54. public class IntegrationTestWFSClient extends WFSClient {
  55. private URL baseDirectory;
  56. private Map<QName, Diff> diffs = new HashMap<QName, Diff>();
  57. private Map<QName, SimpleFeatureType> featureTypes = new HashMap<QName, SimpleFeatureType>();
  58. public IntegrationTestWFSClient(String baseDirectory, WFSConfig config)
  59. throws ServiceException, IOException {
  60. super(url(baseDirectory + "/GetCapabilities.xml"), new SimpleHttpClient(), config);
  61. this.baseDirectory = url(baseDirectory);
  62. }
  63. private static URL url(String resource) {
  64. String absoluteResouce = "/org/geotools/data/wfs/impl/test-data/" + resource;
  65. URL url = IntegrationTestWFSClient.class.getResource(absoluteResouce);
  66. if(null == url){
  67. throw new IllegalArgumentException("Resource not found: " + absoluteResouce);
  68. }
  69. return url;
  70. }
  71. @Override
  72. protected Response internalIssueRequest(Request request) throws IOException {
  73. try {
  74. if (request instanceof GetCapabilitiesRequest) {
  75. return mockCapabilities();
  76. }
  77. if (request instanceof DescribeFeatureTypeRequest) {
  78. return mockDFT((DescribeFeatureTypeRequest) request);
  79. }
  80. if (request instanceof GetFeatureRequest) {
  81. return mockGetFeature((GetFeatureRequest) request);
  82. }
  83. if (request instanceof TransactionRequest) {
  84. return mockTransaction((TransactionRequest) request);
  85. }
  86. } catch (Exception e) {
  87. e.printStackTrace();
  88. throw new IOException(e.getCause());
  89. }
  90. throw new IllegalArgumentException("Unknown request : " + request);
  91. }
  92. private Response mockCapabilities() throws IOException, ServiceException {
  93. HTTPResponse httpResp = new TestHttpResponse("text/xml", "UTF-8", super.serverURL);
  94. return new GetCapabilitiesResponse(httpResp);
  95. }
  96. private Response mockDFT(DescribeFeatureTypeRequest request) throws ServiceException,
  97. IOException {
  98. QName typeName = request.getTypeName();
  99. String simpleName = typeName.getPrefix() + "_" + typeName.getLocalPart();
  100. String resource = "DescribeFeatureType_" + simpleName + ".xsd";
  101. URL contentUrl = new URL(baseDirectory, resource);
  102. String outputFormat = request.getOutputFormat();
  103. HTTPResponse response = new TestHttpResponse(outputFormat, "UTF-8", contentUrl);
  104. DescribeFeatureTypeResponse ret = new DescribeFeatureTypeResponse(request, response);
  105. FeatureType featureType = ret.getFeatureType();
  106. this.featureTypes.put(typeName, (SimpleFeatureType) featureType);
  107. return ret;
  108. }
  109. private Response mockGetFeature(GetFeatureRequest request) throws IOException {
  110. final QName typeName = request.getTypeName();
  111. String simpleName = typeName.getPrefix() + "_" + typeName.getLocalPart();
  112. String resource = "GetFeature_" + simpleName + ".xml";
  113. URL contentUrl = new URL(baseDirectory, resource);
  114. String outputFormat = request.getOutputFormat();
  115. HTTPResponse httpResponse = new TestHttpResponse(outputFormat, "UTF-8", contentUrl);
  116. WFSResponse response = request.createResponse(httpResponse);
  117. if (!(response instanceof GetFeatureResponse)) {
  118. return response;
  119. }
  120. final GetFeatureResponse gfr = (GetFeatureResponse) response;
  121. final GetFeatureParser allFeatures = gfr.getFeatures();
  122. final List<SimpleFeature> originalFeatures = new ArrayList<SimpleFeature>();
  123. {
  124. SimpleFeature feature;
  125. while ((feature = allFeatures.parse()) != null) {
  126. originalFeatures.add(feature);
  127. }
  128. }
  129. WFSStrategy strategy = getStrategy();
  130. final Filter serverFiler = ((AbstractWFSStrategy) strategy).splitFilters(typeName,
  131. request.getFilter())[0];
  132. final Diff diff = diff(typeName);
  133. for (Iterator<SimpleFeature> it = originalFeatures.iterator(); it.hasNext();) {
  134. if (!serverFiler.evaluate(it.next())) {
  135. it.remove();
  136. }
  137. }
  138. FeatureReader<SimpleFeatureType, SimpleFeature> allFeaturesReader;
  139. allFeaturesReader = DataUtilities.reader(originalFeatures);
  140. final DiffFeatureReader<SimpleFeatureType, SimpleFeature> serverFilteredReader;
  141. serverFilteredReader = new DiffFeatureReader<SimpleFeatureType, SimpleFeature>(
  142. allFeaturesReader, diff);
  143. final GetFeatureParser filteredParser = new GetFeatureParser() {
  144. @Override
  145. public void setGeometryFactory(GeometryFactory geometryFactory) {
  146. // TODO Auto-generated method stub
  147. }
  148. @Override
  149. public SimpleFeature parse() throws IOException {
  150. if (!serverFilteredReader.hasNext()) {
  151. return null;
  152. }
  153. return serverFilteredReader.next();
  154. }
  155. @Override
  156. public int getNumberOfFeatures() {
  157. if (-1 != allFeatures.getNumberOfFeatures()) {
  158. // only if the original response included number of features (i.e. the server
  159. // does advertise it)
  160. FeatureReader<SimpleFeatureType, SimpleFeature> all;
  161. try {
  162. all = DataUtilities.reader(originalFeatures);
  163. final DiffFeatureReader<SimpleFeatureType, SimpleFeature> serverFiltered;
  164. serverFiltered = new DiffFeatureReader<SimpleFeatureType, SimpleFeature>(
  165. all, diff);
  166. int count = 0;
  167. while (serverFiltered.hasNext()) {
  168. serverFiltered.next();
  169. count++;
  170. }
  171. return count;
  172. } catch (Exception e) {
  173. e.printStackTrace();
  174. throw new RuntimeException(e);
  175. }
  176. }
  177. return -1;
  178. }
  179. @Override
  180. public FeatureType getFeatureType() {
  181. return allFeatures.getFeatureType();
  182. }
  183. @Override
  184. public void close() throws IOException {
  185. //
  186. }
  187. };
  188. try {
  189. return new GetFeatureResponse(request, httpResponse, filteredParser);
  190. } catch (ServiceException e) {
  191. throw new IOException(e);
  192. }
  193. }
  194. private AtomicInteger idseq = new AtomicInteger();
  195. private Response mockTransaction(TransactionRequest request) throws Exception {
  196. List<String> added = new ArrayList<String>();
  197. int deleted = 0, updated = 0;
  198. for (TransactionElement e : request.getTransactionElements()) {
  199. QName typeName = e.getTypeName();
  200. if (e instanceof Insert) {
  201. Diff diff = diff(typeName);
  202. for (SimpleFeature f : ((Insert) e).getFeatures()) {
  203. String newId = "wfs-generated-" + idseq.incrementAndGet();
  204. diff.add(f.getID(), f);
  205. added.add(newId);
  206. }
  207. }
  208. if (e instanceof Delete) {
  209. Diff diff = diff(typeName);
  210. Filter filter = ((Delete) e).getFilter();
  211. List<SimpleFeature> features = features(typeName);
  212. for (SimpleFeature f : features) {
  213. if (filter.evaluate(f)) {
  214. diff.remove(f.getID());
  215. deleted++;
  216. }
  217. }
  218. }
  219. if (e instanceof Update) {
  220. Diff diff = diff(typeName);
  221. Update u = (Update) e;
  222. Filter filter = u.getFilter();
  223. List<SimpleFeature> features = features(typeName);
  224. List<QName> propertyNames = u.getPropertyNames();
  225. List<Object> newValues = u.getNewValues();
  226. for (SimpleFeature f : features) {
  227. if (!filter.evaluate(f)) {
  228. continue;
  229. }
  230. for (int i = 0; i < propertyNames.size(); i++) {
  231. QName propName = propertyNames.get(i);
  232. Object value = newValues.get(i);
  233. String attName = propName.getLocalPart();
  234. f.setAttribute(attName, value);
  235. }
  236. diff.modify(f.getID(), f);
  237. updated++;
  238. }
  239. }
  240. }
  241. String outputFormat = request.getOutputFormat();
  242. String responseContents = createTransactionResponseXml(added, updated, deleted);
  243. HTTPResponse httpResponse = new TestHttpResponse(outputFormat, "UTF-8", responseContents);
  244. return request.createResponse(httpResponse);
  245. }
  246. private Diff diff(QName typeName) {
  247. Diff diff = diffs.get(typeName);
  248. if (diff == null) {
  249. diff = new Diff();
  250. diffs.put(typeName, diff);
  251. }
  252. return diff;
  253. }
  254. private List<SimpleFeature> features(QName typeName) throws IOException {
  255. GetFeatureRequest gf = createGetFeatureRequest();
  256. gf.setTypeName(typeName);
  257. SimpleFeatureType featureType = featureTypes.get(typeName);
  258. if (featureType == null) {
  259. throw new IllegalStateException();
  260. }
  261. gf.setFullType(featureType);
  262. gf.setQueryType(featureType);
  263. gf.setFilter(Filter.INCLUDE);
  264. GetFeatureResponse response = (GetFeatureResponse) mockGetFeature(gf);
  265. GetFeatureParser features = response.getFeatures();
  266. List<SimpleFeature> result = new ArrayList<SimpleFeature>();
  267. SimpleFeature f;
  268. while ((f = features.parse()) != null) {
  269. result.add(f);
  270. }
  271. return result;
  272. }
  273. @SuppressWarnings("unchecked")
  274. private String createTransactionResponseXml(List<String> added, int updated, int deleted)
  275. throws IOException {
  276. WfsFactory factory = WfsFactory.eINSTANCE;
  277. TransactionResponseType tr = factory.createTransactionResponseType();
  278. tr.setVersion(getStrategy().getVersion());
  279. tr.setTransactionSummary(factory.createTransactionSummaryType());
  280. tr.getTransactionSummary().setTotalInserted(BigInteger.valueOf(added.size()));
  281. tr.getTransactionSummary().setTotalUpdated(BigInteger.valueOf(updated));
  282. tr.getTransactionSummary().setTotalDeleted(BigInteger.valueOf(deleted));
  283. tr.setTransactionResults(factory.createTransactionResultsType());
  284. tr.setInsertResults(factory.createInsertResultsType());
  285. if (!added.isEmpty()) {
  286. FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
  287. InsertedFeatureType inserted = factory.createInsertedFeatureType();
  288. tr.getInsertResults().getFeature().add(inserted);
  289. for (String addedId : added) {
  290. FeatureId featureId = ff.featureId(addedId);
  291. inserted.getFeatureId().add(featureId);
  292. }
  293. }
  294. Configuration configuration = getStrategy().getWfsConfiguration();
  295. Encoder enc = new Encoder(configuration);
  296. enc.setEncoding(Charset.forName("UTF-8"));
  297. enc.setIndenting(true);
  298. enc.setIndentSize(1);
  299. String encodedTransactionResponse = enc.encodeAsString(tr, WFS.TransactionResponse);
  300. System.err.println(encodedTransactionResponse);
  301. return encodedTransactionResponse;
  302. }
  303. }