Search in sources :

Example 1 with AttachmentDetails

use of com.twinsoft.convertigo.engine.AttachmentManager.AttachmentDetails in project convertigo by convertigo.

the class WebServiceTranslator method addSoapElement.

private SOAPElement addSoapElement(Context context, SOAPEnvelope se, SOAPElement soapParent, Node node) throws Exception {
    String prefix = node.getPrefix();
    String namespace = node.getNamespaceURI();
    String nodeName = node.getNodeName();
    String localName = node.getLocalName();
    String value = node.getNodeValue();
    boolean includeResponseElement = true;
    if (context.sequenceName != null) {
        includeResponseElement = ((Sequence) context.requestedObject).isIncludeResponseElement();
    }
    SOAPElement soapElement = null;
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element element = (Element) node;
        boolean toAdd = true;
        if (!includeResponseElement && "response".equalsIgnoreCase(localName)) {
            toAdd = false;
        }
        if ("http://schemas.xmlsoap.org/soap/envelope/".equals(element.getParentNode().getNamespaceURI()) || "http://schemas.xmlsoap.org/soap/envelope/".equals(namespace) || nodeName.toLowerCase().endsWith(":envelope") || nodeName.toLowerCase().endsWith(":body")) // element.getParentNode().getNodeName().toUpperCase().indexOf("NS0:") != -1 ||
        // nodeName.toUpperCase().indexOf("NS0:") != -1
        {
            toAdd = false;
        }
        if (toAdd) {
            if (prefix == null || prefix.equals("")) {
                soapElement = soapParent.addChildElement(nodeName);
            } else {
                soapElement = soapParent.addChildElement(se.createName(localName, prefix, namespace));
            }
        } else {
            soapElement = soapParent;
        }
        if (soapElement != null) {
            if (soapParent.equals(se.getBody()) && !soapParent.equals(soapElement)) {
                if (XsdForm.qualified == context.project.getSchemaElementForm()) {
                    if (soapElement.getAttribute("xmlns") == null) {
                        soapElement.addAttribute(se.createName("xmlns"), context.project.getTargetNamespace());
                    }
                }
            }
            if (element.hasAttributes()) {
                String attrType = element.getAttribute("type");
                if (("attachment").equals(attrType)) {
                    if (context.requestedObject instanceof AbstractHttpTransaction) {
                        AttachmentDetails attachment = AttachmentManager.getAttachment(element);
                        if (attachment != null) {
                            byte[] raw = attachment.getData();
                            if (raw != null)
                                soapElement.addTextNode(Base64.encodeBase64String(raw));
                        }
                    /* DON'T WORK YET *\
	    					AttachmentPart ap = responseMessage.createAttachmentPart(new ByteArrayInputStream(raw), element.getAttribute("content-type"));
	    					ap.setContentId(key);
	    					ap.setContentLocation(element.getAttribute("url"));
	    					responseMessage.addAttachmentPart(ap);
	    					\* DON'T WORK YET */
                    }
                }
                if (!includeResponseElement && "response".equalsIgnoreCase(localName)) {
                // do not add attributes
                } else {
                    NamedNodeMap attributes = element.getAttributes();
                    int len = attributes.getLength();
                    for (int i = 0; i < len; i++) {
                        Node item = attributes.item(i);
                        addSoapElement(context, se, soapElement, item);
                    }
                }
            }
            if (element.hasChildNodes()) {
                NodeList childNodes = element.getChildNodes();
                int len = childNodes.getLength();
                for (int i = 0; i < len; i++) {
                    Node item = childNodes.item(i);
                    switch(item.getNodeType()) {
                        case Node.ELEMENT_NODE:
                            addSoapElement(context, se, soapElement, item);
                            break;
                        case Node.CDATA_SECTION_NODE:
                        case Node.TEXT_NODE:
                            String text = item.getNodeValue();
                            text = (text == null) ? "" : text;
                            soapElement.addTextNode(text);
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    } else if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
        if (prefix == null || prefix.equals("")) {
            soapElement = soapParent.addAttribute(se.createName(nodeName), value);
        } else {
            soapElement = soapParent.addAttribute(se.createName(localName, prefix, namespace), value);
        }
    }
    return soapElement;
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) SOAPElement(javax.xml.soap.SOAPElement) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) SOAPElement(javax.xml.soap.SOAPElement) AttachmentDetails(com.twinsoft.convertigo.engine.AttachmentManager.AttachmentDetails) AbstractHttpTransaction(com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction)

Example 2 with AttachmentDetails

use of com.twinsoft.convertigo.engine.AttachmentManager.AttachmentDetails 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)

Example 3 with AttachmentDetails

use of com.twinsoft.convertigo.engine.AttachmentManager.AttachmentDetails in project convertigo by convertigo.

the class WebServiceTranslator method addElement.

private void addElement(SOAPMessage responseMessage, SOAPEnvelope soapEnvelope, Context context, Element elementToAdd, SOAPElement soapElement) throws SOAPException {
    SOAPElement soapMethodResponseElement = (SOAPElement) soapEnvelope.getBody().getFirstChild();
    String targetNamespace = soapMethodResponseElement.getNamespaceURI();
    String prefix = soapMethodResponseElement.getPrefix();
    String nodeType = elementToAdd.getAttribute("type");
    SOAPElement childSoapElement = soapElement;
    boolean elementAdded = true;
    boolean bTable = false;
    if (nodeType.equals("table")) {
        bTable = true;
        /*childSoapElement = soapElement.addChildElement("ArrayOf" + context.transactionName + "_" + tableName + "_Row", "");

            if (!context.httpServletRequest.getServletPath().endsWith(".wsl")) {
            	childSoapElement.addAttribute(soapEnvelope.createName("xsi:type"), "soapenc:Array");
            }*/
        childSoapElement = soapElement.addChildElement(elementToAdd.getNodeName());
    } else if (nodeType.equals("row")) {
        /*String elementType = context.transactionName + "_" + tableName + "_Row";
			childSoapElement = soapElement.addChildElement(elementType, "");*/
        childSoapElement = soapElement.addChildElement(elementToAdd.getNodeName());
    } else if (nodeType.equals("attachment")) {
        childSoapElement = soapElement.addChildElement(elementToAdd.getNodeName());
        if (context.requestedObject instanceof AbstractHttpTransaction) {
            AttachmentDetails attachment = AttachmentManager.getAttachment(elementToAdd);
            if (attachment != null) {
                byte[] raw = attachment.getData();
                if (raw != null)
                    childSoapElement.addTextNode(Base64.encodeBase64String(raw));
            }
        /* DON'T WORK YET *\
				AttachmentPart ap = responseMessage.createAttachmentPart(new ByteArrayInputStream(raw), elementToAdd.getAttribute("content-type"));
				ap.setContentId(key);
				ap.setContentLocation(elementToAdd.getAttribute("url"));
				responseMessage.addAttachmentPart(ap);
				\* DON'T WORK YET */
        }
    } else {
        String elementNodeName = elementToAdd.getNodeName();
        String elementNodeNsUri = elementToAdd.getNamespaceURI();
        String elementNodePrefix = getPrefix(context.projectName, elementNodeNsUri);
        XmlSchemaElement xmlSchemaElement = getXmlSchemaElementByName(context.projectName, elementNodeName);
        boolean isGlobal = xmlSchemaElement != null;
        if (isGlobal) {
            elementNodeNsUri = xmlSchemaElement.getQName().getNamespaceURI();
            elementNodePrefix = getPrefix(context.projectName, elementNodeNsUri);
        }
        // }
        if ("http://schemas.xmlsoap.org/soap/envelope/".equals(elementToAdd.getNamespaceURI()) || "http://schemas.xmlsoap.org/soap/envelope/".equals(elementToAdd.getParentNode().getNamespaceURI()) || elementToAdd.getParentNode().getNodeName().toUpperCase().indexOf("NS0:") != -1 || elementNodeName.toUpperCase().indexOf("NS0:") != -1) {
            elementAdded = false;
        } else {
            if (XsdForm.qualified == context.project.getSchemaElementForm() || isGlobal) {
                if (elementNodePrefix == null) {
                    childSoapElement = soapElement.addChildElement(soapEnvelope.createName(elementNodeName, prefix, targetNamespace));
                } else {
                    childSoapElement = soapElement.addChildElement(soapEnvelope.createName(elementNodeName, elementNodePrefix, elementNodeNsUri));
                }
            } else {
                childSoapElement = soapElement.addChildElement(elementNodeName);
            }
        }
    }
    if (elementAdded && elementToAdd.hasAttributes()) {
        addAttributes(responseMessage, soapEnvelope, context, elementToAdd.getAttributes(), childSoapElement);
    }
    if (elementToAdd.hasChildNodes()) {
        NodeList childNodes = elementToAdd.getChildNodes();
        int len = childNodes.getLength();
        if (bTable) {
        /*if (!context.httpServletRequest.getServletPath().endsWith(".wsl")) {
	            	childSoapElement.addAttribute(soapEnvelope.createName("soapenc:arrayType"), context.projectName+"_ns:" + context.transactionName + "_" + tableName + "_Row[" + (len - 1) + "]");
	            }*/
        }
        org.w3c.dom.Node node;
        Element childElement;
        for (int i = 0; i < len; i++) {
            node = childNodes.item(i);
            switch(node.getNodeType()) {
                case org.w3c.dom.Node.ELEMENT_NODE:
                    childElement = (Element) node;
                    addElement(responseMessage, soapEnvelope, context, childElement, childSoapElement);
                    break;
                case org.w3c.dom.Node.CDATA_SECTION_NODE:
                case org.w3c.dom.Node.TEXT_NODE:
                    String text = node.getNodeValue();
                    text = (text == null) ? "" : text;
                    childSoapElement.addTextNode(text);
                    break;
                default:
                    break;
            }
        }
    /*org.w3c.dom.Node node;
			Element childElement;
			for (int i = 0 ; i < len ; i++) {
				node = childNodes.item(i);
				if (node instanceof Element) {
					childElement = (Element) node;
					addElement(responseMessage, soapEnvelope, context, childElement, childSoapElement);
				}
				else if (node instanceof CDATASection) {
					Node textNode = XMLUtils.findChildNode(elementToAdd, org.w3c.dom.Node.CDATA_SECTION_NODE);
					String text = textNode.getNodeValue();
					if (text == null) {
						text = "";
					}
					childSoapElement.addTextNode(text);
				}
				else {
					Node textNode = XMLUtils.findChildNode(elementToAdd, org.w3c.dom.Node.TEXT_NODE);
					if (textNode != null) {
						String text = textNode.getNodeValue();
						if (text == null) {
							text = "";
						}
						childSoapElement.addTextNode(text);
					}
				}
			}*/
    }
}
Also used : XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) NodeList(org.w3c.dom.NodeList) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) SOAPElement(javax.xml.soap.SOAPElement) Element(org.w3c.dom.Element) AbstractHttpTransaction(com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction) Node(org.w3c.dom.Node) SOAPElement(javax.xml.soap.SOAPElement) AttachmentDetails(com.twinsoft.convertigo.engine.AttachmentManager.AttachmentDetails)

Aggregations

AttachmentDetails (com.twinsoft.convertigo.engine.AttachmentManager.AttachmentDetails)3 AbstractHttpTransaction (com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction)2 SOAPElement (javax.xml.soap.SOAPElement)2 XmlSchemaElement (org.apache.ws.commons.schema.XmlSchemaElement)2 Element (org.w3c.dom.Element)2 Node (org.w3c.dom.Node)2 NodeList (org.w3c.dom.NodeList)2 Project (com.twinsoft.convertigo.beans.core.Project)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 NamedNodeMap (org.w3c.dom.NamedNodeMap)1