params = buildParameterDictionary(requestBody);
LOG.trace("Request parameters: {}", params);
request = urldecode(params.get("request"), httpRequest.getContentType());
// WPS 1.0.0 GET interface processing
if ((httpRequest.getParameter("Service") != null) && (httpRequest.getParameter("Service").equalsIgnoreCase("WPS"))) {
WpsServer wpsServer = new WpsServer(httpResponse, httpRequest);
request = wpsServer.request;
}
// To preserve compatibility with previous client versions, we allow
// GET requests with parameter "query"
String request2 = null;
request2 = httpRequest.getParameter("query");
if (request2 == null) {
request2 = urldecode(params.get("query"), httpRequest.getContentType());
}
if (request2 != null) {
LOG.debug("Received Abstract Syntax Request via GET: \n\t\t{}", request2);
request2 = ProcessCoveragesRequest.abstractQueryToXmlQuery(request2);
}
if (request == null && request2 != null) {
request = request2;
}
// Empty request ?
if (request == null && (requestBody == null || requestBody.length() == 0)) {
printUsage(httpResponse, request);
return;
}
// No parameters, just XML in the request body
if (request == null && requestBody != null && requestBody.length() > 0) {
request = urldecode(requestBody, httpRequest.getContentType());
// if (request.matches(" *<.*") == false)
// {
// handleUnknownRequest(request, httpResponse);
// return;
// }
}
LOG.debug("Petascope Request: \n------START REQUEST--------\n"
+ request + "\n------END REQUEST------\n");
Document doc = builder.parse(IOUtils.toInputStream(request));
Element rootElem = doc.getDocumentElement();
String root = rootElem.getTagName();
LOG.debug("Root Element name: {}", root);
String version = "1.1.0";
try {
XPath xpath = xpathFactory.newXPath();
xpath.setNamespaceContext(new WcsNamespaceContext());
String query = "/*/@version";
version = (String) xpath.evaluate(query, doc, XPathConstants.STRING);
} catch (XPathExpressionException e) {
LOG.warn("The root XML node has no attribute called \"version\"");
}
/* The next request types are defined in the WcsServer standard, and
are common to all the PetaScope services. */
if (root.endsWith("GetCapabilities")) {
if (version.startsWith("2")) {
handleGetCapabilities2(request, httpResponse);
} else {
handleGetCapabilities(request, httpResponse);
}
} else if (root.endsWith("DescribeCoverage")) {
if (version.startsWith("2")) {
handleDescribeCoverage2(request, httpResponse);
} else {
handleDescribeCoverage(request, httpResponse);
}
} else if (root.endsWith("GetCoverage")) {
if (version.startsWith("2")) {
handleGetCoverage2(request, httpResponse);
} else {
handleGetCoverage(request, httpResponse);
}
} else /* ProcessCoverages is defined in the WCPS extension to WcsServer */
if (root.endsWith("ProcessCoveragesRequest")) {
handleProcessCoverages(request, httpResponse);
} else /* Transaction is defined in the WcsServer-T extension to WcsServer */
if (root.endsWith("Transaction")) {
handleTransaction(request, httpResponse);
} else /* Print Error Message */ {
handleUnknownRequest(request, httpResponse);
}
} catch (IOException e) {
throw new ServletConnectionException(e.getMessage(), e);
} catch (RecognitionException e) {
throw new InvalidRequestException(e.getMessage(), e);
} catch (SAXException e) {
throw new InvalidRequestException(e.getMessage(), e);
} catch (WCSException e) {
throw e;
} catch (Exception e) {
// Finally, cast all other exceptions into a WCSException
LOG.error("Runtime error : {}", e.getMessage());
throw new WcsRuntimeException(e.getMessage(), e);
}
} // And catch all WCSExceptions, to display to the client
catch (WCSException e) {
printError(httpResponse, request, e);
}
}
private void printUsage(HttpServletResponse httpResponse,
String request) throws IOException {
PrintWriter out = httpResponse.getWriter();
httpResponse.setContentType("text/html");
out.write(usageMessage);
out.flush();
}
private void printError(HttpServletResponse response,
String message, Exception e) {
PrintWriter out;
try {
out = new PrintWriter(response.getOutputStream());
} catch (IOException e1) {
LOG.error("Could not print exception because of IO error. Stack trace:", e1);
return;
}
LOG.error("Error stack trace:", e);
if (e instanceof WCSException) {
// We can send an error report
String output = exceptionToXml((WCSException) e);
response.setContentType("text/xml; charset=utf-8");
out.println(output);
out.close();
} else {
LOG.trace("setting response mimetype to text/html; charset=utf-8");
response.setContentType("text/html; charset=utf-8");
LOG.trace("returning the following error message.", e);
LOG.trace("end of error message");
out.println(
"PetaScope");
out.println("An error has occured
");
out.println("" + message + "
");
out.println("Stack trace:
");
e.printStackTrace(out);
out.println("
");
out.close();
LOG.trace("done with error");
}
}
private void handleUnknownRequest(String request, HttpServletResponse httpResponse) {
request = "'" + request + "'";
WCSException e = new NoApplicableCodeException("Could not understand request " + request);
printError(httpResponse, request, e);
}
private String exceptionReportToXml(ExceptionReport report) {
String output = null;
try {
javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(report.getClass().getPackage().getName());
javax.xml.bind.Marshaller marshaller = jaxbCtx.createMarshaller();
marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8"); //NOI18N
marshaller.setProperty("jaxb.formatted.output", true);
marshaller.setProperty("jaxb.schemaLocation",
"http://www.opengis.net/ows http://schemas.opengis.net/ows/1.0.0/owsExceptionReport.xsd");
marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new PetascopeXmlNamespaceMapper());
StringWriter strWriter = new StringWriter();
marshaller.marshal(report, strWriter);
output = strWriter.toString();
System.err.println(output);
LOG.debug("Done marshalling Error Report.");
} catch (JAXBException e2) {
LOG.error("Stack trace: {}", e2);
LOG.error("Error stack trace: " + e2);
}
return output;
}
private String exceptionToXml(WCSException e) {
return exceptionReportToXml(e.getReport());
}
/**
* GetCapabilities of WCS 1.1
* @param request
* @param httpResponse
* @throws WCSException
*/
private void handleGetCapabilities(String request, HttpServletResponse httpResponse) throws WCSException {
String output = wcs.GetCapabilities(request);
PrintWriter out;
try {
out = httpResponse.getWriter();
httpResponse.setContentType("text/xml; charset=utf-8");
out.write(output);
out.flush();
out.close();
} catch (IOException e) {
throw new InputOutputException(e.getMessage(), e);
}
}
/**
* GetCapabilities of WCS 2.0
* @param request
* @param httpResponse
* @throws WCSException
*/
private void handleGetCapabilities2(String request, HttpServletResponse httpResponse) throws WCSException {
String output = wcs2.GetCapabilities(request);
PrintWriter out;
try {
out = httpResponse.getWriter();
httpResponse.setContentType("text/xml; charset=utf-8");
out.write(output);
out.flush();
out.close();
} catch (IOException e) {
throw new InputOutputException(e.getMessage(), e);
}
}
/**
* DescribeCoverage for WCS 1.1
* @param request
* @param httpResponse
* @throws WCSException
*/
private void handleDescribeCoverage(String request, HttpServletResponse httpResponse) throws WCSException {
String output = wcs.DescribeCoverage(request);
PrintWriter out;
try {
out = httpResponse.getWriter();
httpResponse.setContentType("text/xml; charset=utf-8");
out.write(output);
out.flush();
out.close();
} catch (IOException e) {
throw new InputOutputException(e.getMessage(), e);
}
}
/**
* Describe Coverage for WCS 2.0
* @param request
* @param httpResponse
* @throws WCSException
*/
private void handleDescribeCoverage2(String request, HttpServletResponse httpResponse) throws WCSException {
String output = wcs2.DescribeCoverage(request);
PrintWriter out;
try {
out = httpResponse.getWriter();
httpResponse.setContentType("text/xml; charset=utf-8");
out.write(output);
out.flush();
out.close();
} catch (IOException e) {
throw new InputOutputException(e.getMessage(), e);
}
}
private void handleGetCoverage(String request, HttpServletResponse httpResponse) throws WCSException, InvalidCrsException {
String xmlRequest = wcs.GetCoverage(request, wcps);
LOG.debug("Received GetCoverage Request: \n{}", xmlRequest);
// Redirect the request to WCPS
handleProcessCoverages(xmlRequest, httpResponse);
}
private void handleGetCoverage2(String request, HttpServletResponse httpResponse) throws WCSException {
String output = wcs2.GetCoverage(request);
PrintWriter out;
try {
out = httpResponse.getWriter();
httpResponse.setContentType("text/xml; charset=utf-8");
out.write(output);
out.flush();
out.close();
} catch (IOException e) {
throw new InputOutputException(e.getMessage(), e);
}
}
private void handleProcessCoverages(String xmlRequest, HttpServletResponse response) throws WCSException, InvalidCrsException {
OutputStream webOut = null;
try {
LOG.debug("Received a ProcessCoverages request: \n{}", xmlRequest);
LOG.debug("WCPS: preparing request");
ProcessCoveragesRequest processCoverageRequest =
wcps.pcPrepare(ConfigManager.RASDAMAN_URL, ConfigManager.RASDAMAN_DATABASE,
IOUtils.toInputStream(xmlRequest));
String query = processCoverageRequest.getRasqlQuery();
String mime = processCoverageRequest.getMime();
LOG.debug("Resulting RasQL query: [{}] {}", mime, query);
LOG.trace("WCPS: executing request");
List results = processCoverageRequest.execute();
LOG.debug("WCPS: setting response mimetype to " + mime);
response.setContentType(mime);
LOG.trace("WCPS: returning response");
webOut = response.getOutputStream();
if (results.size() > 0) {
webOut.write(results.get(0));
if (ConfigManager.CCIP_HACK == true) {
try {
String dir = getServletContext().getRealPath("/");
File f = new File(dir + "image.jpeg");
LOG.info("HACK: Writing image to: " + f.getAbsolutePath());
{
OutputStream os = new DataOutputStream(new FileOutputStream(f, false));
os.write(results.get(0));
os.close();
LOG.info("HACK: Wrote image successfully !");
}
} catch (Exception e) {
LOG.warn("Error while evaluating CCIP hack: '{}'", e.getMessage());
}
}
} else {
LOG.warn("WCPS: Warning! No result returned from rasql query.");
}
LOG.debug("WCPS: done");
} catch (WCPSException e) {
throw new InternalComponentException(e.getMessage(), e);
} catch (SAXException e) {
throw new XmlNotValidException(e.getMessage(), e);
} catch (IOException e) {
throw new InputOutputException(e.getMessage(), e);
} finally {
if (webOut != null) {
try {
webOut.close();
} catch (IOException e) {
}
}
}
}
private void handleTransaction(String request, HttpServletResponse httpResponse) throws WCSException {
try {
String outputXml = wcst.Transaction(request);
PrintWriter out = new PrintWriter(httpResponse.getOutputStream());
httpResponse.setContentType("text/xml; charset=utf-8");
out.write(outputXml);
out.flush();
} catch (IOException e) {
throw new InputOutputException(e.getMessage(), e);
}
}
}