Example usage for javax.xml.ws.handler MessageContext HTTP_REQUEST_METHOD

List of usage examples for javax.xml.ws.handler MessageContext HTTP_REQUEST_METHOD

Introduction

In this page you can find the example usage for javax.xml.ws.handler MessageContext HTTP_REQUEST_METHOD.

Prototype

String HTTP_REQUEST_METHOD

To view the source code for javax.xml.ws.handler MessageContext HTTP_REQUEST_METHOD.

Click Source Link

Document

Standard property: HTTP request method.

Usage

From source file:com.wavemaker.runtime.ws.HTTPBindingSupport.java

@SuppressWarnings("unchecked")
private static <T extends Object> T getResponse(QName serviceQName, QName portQName, String endpointAddress,
        HTTPRequestMethod method, T postSource, BindingProperties bindingProperties, Class<T> type,
        Map<String, Object> headerParams) throws WebServiceException {

    Service service = Service.create(serviceQName);
    URI endpointURI;/*w w  w .jav a 2s  .  c  o  m*/
    try {
        if (bindingProperties != null) {
            // if BindingProperties had endpointAddress defined, then use
            // it instead of the endpointAddress passed in from arguments.
            String endAddress = bindingProperties.getEndpointAddress();
            if (endAddress != null) {
                endpointAddress = endAddress;
            }
        }
        endpointURI = new URI(endpointAddress);
    } catch (URISyntaxException e) {
        throw new WebServiceException(e);
    }

    String endpointPath = null;
    String endpointQueryString = null;
    if (endpointURI != null) {
        endpointPath = endpointURI.getRawPath();
        endpointQueryString = endpointURI.getRawQuery();
    }

    service.addPort(portQName, HTTPBinding.HTTP_BINDING, endpointAddress);

    Dispatch<T> d = service.createDispatch(portQName, type, Service.Mode.MESSAGE);

    Map<String, Object> requestContext = d.getRequestContext();
    requestContext.put(MessageContext.HTTP_REQUEST_METHOD, method.toString());
    requestContext.put(MessageContext.QUERY_STRING, endpointQueryString);
    requestContext.put(MessageContext.PATH_INFO, endpointPath);

    Map<String, List<String>> reqHeaders = null;
    if (bindingProperties != null) {
        String httpBasicAuthUsername = bindingProperties.getHttpBasicAuthUsername();
        if (httpBasicAuthUsername != null) {
            requestContext.put(BindingProvider.USERNAME_PROPERTY, httpBasicAuthUsername);
            String httpBasicAuthPassword = bindingProperties.getHttpBasicAuthPassword();
            requestContext.put(BindingProvider.PASSWORD_PROPERTY, httpBasicAuthPassword);
        }

        int connectionTimeout = bindingProperties.getConnectionTimeout();
        requestContext.put(JAXWSProperties.CONNECT_TIMEOUT, Integer.valueOf(connectionTimeout));

        int requestTimeout = bindingProperties.getRequestTimeout();
        requestContext.put(JAXWSProperties.REQUEST_TIMEOUT, Integer.valueOf(requestTimeout));

        Map<String, List<String>> httpHeaders = bindingProperties.getHttpHeaders();
        if (httpHeaders != null && !httpHeaders.isEmpty()) {
            reqHeaders = (Map<String, List<String>>) requestContext.get(MessageContext.HTTP_REQUEST_HEADERS);
            if (reqHeaders == null) {
                reqHeaders = new HashMap<String, List<String>>();
                requestContext.put(MessageContext.HTTP_REQUEST_HEADERS, reqHeaders);
            }
            for (Entry<String, List<String>> entry : httpHeaders.entrySet()) {
                reqHeaders.put(entry.getKey(), entry.getValue());
            }
        }
    }

    // Parameters to pass in http header
    if (headerParams != null && headerParams.size() > 0) {
        if (null == reqHeaders) {
            reqHeaders = new HashMap<String, List<String>>();
        }
        Set<Entry<String, Object>> entries = headerParams.entrySet();
        for (Map.Entry<String, Object> entry : entries) {
            List<String> valList = new ArrayList<String>();
            valList.add((String) entry.getValue());
            reqHeaders.put(entry.getKey(), valList);
            requestContext.put(MessageContext.HTTP_REQUEST_HEADERS, reqHeaders);
        }
    }

    logger.info("Invoking HTTP '" + method + "' request with URL: " + endpointAddress);

    T result = d.invoke(postSource);
    return result;
}

From source file:nl.nn.adapterframework.extensions.cxf.SOAPProviderBase.java

@Override
public SOAPMessage invoke(SOAPMessage request) {
    String result;//  ww  w . jav a2 s.  c  o  m
    PipeLineSessionBase pipelineSession = new PipeLineSessionBase();
    String correlationId = Misc.createSimpleUUID();
    log.debug(getLogPrefix(correlationId) + "received message");

    if (request == null) {
        String faultcode = "soap:Server";
        String faultstring = "SOAPMessage is null";
        String httpRequestMethod = (String) webServiceContext.getMessageContext()
                .get(MessageContext.HTTP_REQUEST_METHOD);
        if (!"POST".equals(httpRequestMethod)) {
            faultcode = "soap:Client";
            faultstring = "Request was send using '" + httpRequestMethod + "' instead of 'POST'";
        }
        result = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                + "<soap:Body><soap:Fault>" + "<faultcode>" + faultcode + "</faultcode>" + "<faultstring>"
                + faultstring + "</faultstring>" + "</soap:Fault></soap:Body></soap:Envelope>";
    } else {
        // Make mime headers in request available as session key
        @SuppressWarnings("unchecked")
        Iterator<MimeHeader> mimeHeaders = request.getMimeHeaders().getAllHeaders();
        String mimeHeadersXml = getMimeHeadersXml(mimeHeaders).toXML();
        pipelineSession.put("mimeHeaders", mimeHeadersXml);

        // Make attachments in request (when present) available as session keys
        int i = 1;
        XmlBuilder attachments = new XmlBuilder("attachments");
        @SuppressWarnings("unchecked")
        Iterator<AttachmentPart> attachmentParts = request.getAttachments();
        while (attachmentParts.hasNext()) {
            try {
                InputStreamAttachmentPart attachmentPart = new InputStreamAttachmentPart(
                        attachmentParts.next());

                XmlBuilder attachment = new XmlBuilder("attachment");
                attachments.addSubElement(attachment);
                XmlBuilder sessionKey = new XmlBuilder("sessionKey");
                sessionKey.setValue("attachment" + i);
                attachment.addSubElement(sessionKey);
                pipelineSession.put("attachment" + i, attachmentPart.getInputStream());
                log.debug(getLogPrefix(correlationId) + "adding attachment [attachment" + i + "] to session");

                @SuppressWarnings("unchecked")
                Iterator<MimeHeader> attachmentMimeHeaders = attachmentPart.getAllMimeHeaders();
                attachment.addSubElement(getMimeHeadersXml(attachmentMimeHeaders));
            } catch (SOAPException e) {
                e.printStackTrace();
                log.warn("Could not store attachment in session key", e);
            }
            i++;
        }
        pipelineSession.put("attachments", attachments.toXML());

        // Transform SOAP message to string
        String message;
        try {
            message = XmlUtils.nodeToString(request.getSOAPPart());
            log.debug(getLogPrefix(correlationId) + "transforming from SOAP message");
        } catch (TransformerException e) {
            String m = "Could not transform SOAP message to string";
            log.error(m, e);
            throw new WebServiceException(m, e);
        }

        // Process message via WebServiceListener
        ISecurityHandler securityHandler = new WebServiceContextSecurityHandler(webServiceContext);
        pipelineSession.setSecurityHandler(securityHandler);
        pipelineSession.put(IPipeLineSession.HTTP_REQUEST_KEY,
                webServiceContext.getMessageContext().get(MessageContext.SERVLET_REQUEST));
        pipelineSession.put(IPipeLineSession.HTTP_RESPONSE_KEY,
                webServiceContext.getMessageContext().get(MessageContext.SERVLET_RESPONSE));

        try {
            log.debug(getLogPrefix(correlationId) + "processing message");
            result = processRequest(correlationId, message, pipelineSession);
        } catch (ListenerException e) {
            String m = "Could not process SOAP message: " + e.getMessage();
            log.error(m);
            throw new WebServiceException(m, e);
        }
    }

    // Transform result string to SOAP message
    SOAPMessage soapMessage = null;
    try {
        log.debug(getLogPrefix(correlationId) + "transforming to SOAP message");
        soapMessage = getMessageFactory().createMessage();
        StreamSource streamSource = new StreamSource(new StringReader(result));
        soapMessage.getSOAPPart().setContent(streamSource);
    } catch (SOAPException e) {
        String m = "Could not transform string to SOAP message";
        log.error(m);
        throw new WebServiceException(m, e);
    }

    String multipartXml = (String) pipelineSession.get(attachmentXmlSessionKey);
    log.debug(getLogPrefix(correlationId) + "building multipart message with MultipartXmlSessionKey ["
            + multipartXml + "]");
    if (StringUtils.isNotEmpty(multipartXml)) {
        Element partsElement;
        try {
            partsElement = XmlUtils.buildElement(multipartXml);
        } catch (DomBuilderException e) {
            String m = "error building multipart xml";
            log.error(m, e);
            throw new WebServiceException(m, e);
        }
        Collection<Node> parts = XmlUtils.getChildTags(partsElement, "part");
        if (parts == null || parts.size() == 0) {
            log.warn(getLogPrefix(correlationId) + "no part(s) in multipart xml [" + multipartXml + "]");
        } else {
            Iterator<Node> iter = parts.iterator();
            while (iter.hasNext()) {
                Element partElement = (Element) iter.next();
                //String partType = partElement.getAttribute("type");
                String partName = partElement.getAttribute("name");
                String partSessionKey = partElement.getAttribute("sessionKey");
                String partMimeType = partElement.getAttribute("mimeType");
                Object partObject = pipelineSession.get(partSessionKey);
                if (partObject instanceof InputStream) {
                    InputStream fis = (InputStream) partObject;

                    DataHandler dataHander = null;
                    try {
                        dataHander = new DataHandler(new ByteArrayDataSource(fis, partMimeType));
                    } catch (IOException e) {
                        String m = "Unable to add session key '" + partSessionKey + "' as attachment";
                        log.error(m, e);
                        throw new WebServiceException(m, e);
                    }
                    AttachmentPart attachmentPart = soapMessage.createAttachmentPart(dataHander);
                    attachmentPart.setContentId(partName);
                    soapMessage.addAttachmentPart(attachmentPart);

                    log.debug(getLogPrefix(correlationId) + "appended filepart [" + partSessionKey
                            + "] with value [" + partObject + "] and name [" + partName + "]");
                } else { //String
                    String partValue = (String) partObject;

                    DataHandler dataHander = new DataHandler(new ByteArrayDataSource(partValue, partMimeType));
                    AttachmentPart attachmentPart = soapMessage.createAttachmentPart(dataHander);
                    attachmentPart.setContentId(partName);
                    soapMessage.addAttachmentPart(attachmentPart);

                    log.debug(getLogPrefix(correlationId) + "appended stringpart [" + partSessionKey
                            + "] with value [" + partValue + "]");
                }
            }
        }
    }

    return soapMessage;
}