public static void doProcess()

in ch-sxmp/src/main/java/com/cloudhopper/sxmp/servlet/SxmpServletProcessor.java [76:132]


    public static void doProcess(SxmpProcessor processor, InputStream in, PrintWriter out, String uri, String method, String contentType) throws IOException, HttpStatusCodeException {
        // validate the user went to the correct URL
        // E.g. /api/sxmp/1.0
        // find the next position of last / (to extract version)
        int posOfVersion = uri.lastIndexOf('/');
        if (posOfVersion < 0 || posOfVersion+1 >= uri.length()) {
            throw new HttpStatusCodeException(HttpServletResponse.SC_NOT_FOUND, "Bad URL used, could not extract version or no data for version");
        }

        String version = uri.substring(posOfVersion+1);
        //logger.debug("Parsed API Version: " + version);

        // check if the version is supported
        if (!version.equals(SxmpParser.VERSION_1_0) && !version.equals(SxmpParser.VERSION_1_1)) {
            throw new HttpStatusCodeException(HttpServletResponse.SC_BAD_REQUEST, "Unsupported API version in URL");
        }

        // validate the user did a POST
        if (!method.equalsIgnoreCase("POST")) {
            throw new HttpStatusCodeException(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "Only HTTP POST methods are acceptable");
        }

        // validate the user posted "text/xml"
        //logger.debug("Request contentType: " + contentType);
        if (contentType == null || !contentType.toLowerCase().startsWith("text/xml")) {
            throw new HttpStatusCodeException(HttpServletResponse.SC_BAD_REQUEST, "Unsupported Content-Type HTTP Header - Must Be text/xml");
        }

        // create a new session tied to this processor
        SxmpSession session = new SxmpSession(processor, version);

        Response response = null;
        try {
            // process request, get response
            response = session.process(in);
        } catch (Exception e) {
            // any exception thrown in process() should generate a non-200 HTTP status code
            // the exception also would have already been logged in the SxmpSession, so we
            // won't print it here and duplicate it -- any error during processing
            // would actually have returned an ErrorResponse
            throw new HttpStatusCodeException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
        }

        // at this point, we should have a response
        if (response == null) {
            logger.error(fatal, "The response from SxmpSession.process() was null -- should be impossible");
            throw new HttpStatusCodeException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Response was empty");
        }

        try {
            // if we get here, write a response
            SxmpWriter.write(out, response);
        } catch (SxmpErrorException e) {
            logger.error("Error while writing response", e);
            throw new HttpStatusCodeException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Unable to cleanly write response to OutputStream");
        }
    }