01: /*
02: * GeoTools - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2004-2006, GeoTools Project Managment Committee (PMC)
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation;
09: * version 2.1 of the License.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: */
16: package org.geotools.data.ows;
17:
18: import java.io.IOException;
19: import java.io.InputStream;
20:
21: import org.geotools.ows.ServiceException;
22: import org.jdom.JDOMException;
23:
24: /**
25: * Provides a base class for Responses from an OWS. Checks the incoming content
26: * for a ServiceException and parses it if it encounters one.
27: *
28: * @author rgould
29: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/data/ows/Response.java $
30: */
31: public abstract class Response {
32: protected InputStream inputStream;
33: protected String contentType;
34:
35: public Response(String contentType, InputStream inputStream)
36: throws ServiceException, IOException {
37: this .inputStream = inputStream;
38: this .contentType = contentType;
39:
40: /*
41: * Intercept XML ServiceExceptions and throw them
42: */
43: if (contentType.toLowerCase().equals(
44: "application/vnd.ogc.se_xml")) {
45: throw parseException(inputStream);
46: }
47: }
48:
49: public String getContentType() {
50: return contentType;
51: }
52:
53: /**
54: * Returns the InputStream that contains the response from the server.
55: * The contents of this stream vary according to the type of request
56: * that was made, and whether it was successful or not.
57: *
58: * <B>NOTE:</B>
59: * Note that clients using this code are responsible for closing the
60: * InputStream when they are finished with it.
61: *
62: * @return the input stream containing the response from the server
63: */
64: public InputStream getInputStream() {
65: return inputStream;
66: }
67:
68: protected ServiceException parseException(InputStream inputStream)
69: throws IOException {
70: try {
71: return ServiceExceptionParser.parse(inputStream);
72: } catch (JDOMException e) {
73: throw (IOException) new IOException().initCause(e);
74: } finally {
75: inputStream.close();
76: }
77: }
78: }
|