Search in sources :

Example 1 with WebServiceServletRequester

use of com.twinsoft.convertigo.engine.requesters.WebServiceServletRequester in project convertigo by convertigo.

the class GenericServlet method doRequest.

protected void doRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    HttpServletRequestTwsWrapper wrapped_request = new HttpServletRequestTwsWrapper(request);
    request = wrapped_request;
    String baseUrl = getServletBaseUrl(request);
    boolean isProject;
    if ((isProject = baseUrl.contains("/projects/")) || baseUrl.contains("/webclipper/")) {
        long t0 = System.currentTimeMillis();
        try {
            if (EnginePropertiesManager.getPropertyAsBoolean(PropertyName.XSRF_API)) {
                HttpUtils.checkXSRF(request, response);
            }
            String encoded = request.getParameter(Parameter.RsaEncoded.getName());
            if (encoded != null) {
                String query = Engine.theApp.rsaManager.decrypt(encoded, request.getSession());
                wrapped_request.clearParameters();
                wrapped_request.addQuery(query);
            }
            if (isProject && request.getMethod().equalsIgnoreCase("OPTIONS") && Engine.isStarted) {
                Project project = null;
                String projectName = request.getParameter(Parameter.Project.getName());
                if (projectName == null) {
                    projectName = request.getRequestURI().replaceFirst(".*/projects/(.*?)/.*", "$1");
                }
                if (!projectName.contains("/")) {
                    try {
                        project = Engine.theApp.databaseObjectsManager.getOriginalProjectByName(projectName);
                    } catch (Exception e) {
                    }
                }
                if (project == null) {
                    response.setStatus(HttpServletResponse.SC_NOT_FOUND);
                    return;
                }
                HttpUtils.applyFilterCorsHeaders(request, response, project.getCorsOrigin());
                response.setStatus(HttpServletResponse.SC_NO_CONTENT);
                return;
            }
            Object result = processRequest(request);
            response.addHeader("Expires", "-1");
            if (getCacheControl(request).equals("false")) {
                HeaderName.CacheControl.addHeader(response, "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
            }
            HttpUtils.applyCorsHeaders(request, response);
            /**
             * Disabled since #253 : Too much HTML Connector cookies in
             * response header make a tomcat exception
             * http://sourceus.twinsoft.fr/ticket/253 cookies must be in xml
             * if wanted, not in headers
             *
             * Vector cookies = (Vector)
             * request.getAttribute("convertigo.cookies"); for (int i=0;
             * i<cookies.size(); i++) { String sCookie =
             * (String)cookies.elementAt(i);
             * response.addHeader("Set-Cookie", sCookie);
             * Engine.logContext.trace("[GenericServlet] Set-Cookie: " +
             * sCookie); }
             */
            String trSessionId = (String) request.getAttribute("sequence.transaction.sessionid");
            if ((trSessionId != null) && (!trSessionId.equals(""))) {
                response.setHeader("Transaction-JSessionId", trSessionId);
            }
            String requested_content_type = request.getParameter(Parameter.ContentType.getName());
            String content_type = getContentType(request);
            if (requested_content_type != null && !requested_content_type.equals(content_type)) {
                Engine.logEngine.debug("(GenericServlet) Override Content-Type requested to change : " + content_type + " to " + requested_content_type);
                content_type = requested_content_type;
            } else {
                requested_content_type = null;
            }
            response.setContentType(content_type);
            if (content_type.startsWith("text")) {
                String charset = (String) request.getAttribute("convertigo.charset");
                if (charset != null && charset.length() > 0) {
                    response.setCharacterEncoding(charset);
                }
            }
            try {
                if (result != null) {
                    Boolean b = (Boolean) request.getAttribute("convertigo.isErrorDocument");
                    if (b.booleanValue()) {
                        Requester requester = getRequester();
                        boolean bThrowHTTP500 = false;
                        if (requester instanceof WebServiceServletRequester) {
                            bThrowHTTP500 = Boolean.parseBoolean(EnginePropertiesManager.getProperty(EnginePropertiesManager.PropertyName.THROW_HTTP_500_SOAP_FAULT));
                        } else if (requester instanceof ServletRequester) {
                            bThrowHTTP500 = Boolean.parseBoolean(EnginePropertiesManager.getProperty(EnginePropertiesManager.PropertyName.THROW_HTTP_500));
                        }
                        if (bThrowHTTP500) {
                            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                            Engine.logEngine.debug("(GenericServlet) Requested HTTP 500 status code");
                        }
                    } else {
                        applyCustomStatus(request, response);
                    }
                    if (result instanceof AttachmentDetails) {
                        AttachmentDetails attachment = (AttachmentDetails) result;
                        byte[] data = attachment.getData();
                        String contentType = attachment.getContentType();
                        if (requested_content_type != null) {
                            contentType = requested_content_type;
                        }
                        String name = attachment.getName();
                        HeaderName.ContentType.setHeader(response, contentType);
                        HeaderName.ContentLength.setHeader(response, "" + data.length);
                        HeaderName.ContentDisposition.setHeader(response, "attachment; filename=" + name);
                        applyCustomHeaders(request, response);
                        OutputStream out = response.getOutputStream();
                        out.write(data);
                        out.flush();
                    } else if (result instanceof byte[]) {
                        if (requested_content_type != null) {
                            response.setContentType(requested_content_type);
                        } else {
                            response.setContentType(getContentType(request));
                            response.setCharacterEncoding((String) request.getAttribute("convertigo.charset"));
                        }
                        HeaderName.ContentLength.addHeader(response, "" + ((byte[]) result).length);
                        applyCustomHeaders(request, response);
                        OutputStream out = response.getOutputStream();
                        out.write((byte[]) result);
                        out.flush();
                    } else {
                        String sResult = "";
                        if (result instanceof String) {
                            sResult = (String) result;
                        } else if (result instanceof Document) {
                            sResult = XMLUtils.prettyPrintDOM((Document) result);
                        } else if (result instanceof SOAPMessage) {
                            sResult = SOAPUtils.toString((SOAPMessage) result, (String) request.getAttribute("convertigo.charset"));
                        }
                        applyCustomHeaders(request, response);
                        Writer writer = response.getWriter();
                        writer.write(sResult);
                        writer.flush();
                    }
                } else {
                    applyCustomHeaders(request, response);
                    response.setStatus(HttpServletResponse.SC_NO_CONTENT);
                }
            } catch (IOException e) {
                // The connection has probably been reset by peer
                Engine.logContext.warn("[GenericServlet] The connection has probably been reset by peer (IOException): " + e.getMessage());
            } finally {
                onFinally(request);
            }
        } catch (Exception e) {
            Engine.logContext.error("Unable to process the request!", e);
            processException(request, response, e);
        } finally {
            long t1 = System.currentTimeMillis();
            Engine.theApp.pluginsManager.fireHttpServletRequestEnd(request, t0, t1);
        }
    } else {
        // Not a valid Convertigo invocation URL, use retrieve as static
        // resource
        handleStaticData(request, response);
        return;
    }
}
Also used : OutputStream(java.io.OutputStream) IOException(java.io.IOException) Document(org.w3c.dom.Document) SOAPMessage(javax.xml.soap.SOAPMessage) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Project(com.twinsoft.convertigo.beans.core.Project) ServletRequester(com.twinsoft.convertigo.engine.requesters.ServletRequester) WebServiceServletRequester(com.twinsoft.convertigo.engine.requesters.WebServiceServletRequester) ServletRequester(com.twinsoft.convertigo.engine.requesters.ServletRequester) Requester(com.twinsoft.convertigo.engine.requesters.Requester) WebServiceServletRequester(com.twinsoft.convertigo.engine.requesters.WebServiceServletRequester) AttachmentDetails(com.twinsoft.convertigo.engine.AttachmentManager.AttachmentDetails) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) HttpServletRequestTwsWrapper(com.twinsoft.convertigo.engine.util.HttpServletRequestTwsWrapper) WebServiceServletRequester(com.twinsoft.convertigo.engine.requesters.WebServiceServletRequester)

Aggregations

Project (com.twinsoft.convertigo.beans.core.Project)1 AttachmentDetails (com.twinsoft.convertigo.engine.AttachmentManager.AttachmentDetails)1 Requester (com.twinsoft.convertigo.engine.requesters.Requester)1 ServletRequester (com.twinsoft.convertigo.engine.requesters.ServletRequester)1 WebServiceServletRequester (com.twinsoft.convertigo.engine.requesters.WebServiceServletRequester)1 HttpServletRequestTwsWrapper (com.twinsoft.convertigo.engine.util.HttpServletRequestTwsWrapper)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 PrintWriter (java.io.PrintWriter)1 Writer (java.io.Writer)1 ServletException (javax.servlet.ServletException)1 SOAPMessage (javax.xml.soap.SOAPMessage)1 Document (org.w3c.dom.Document)1