Search in sources :

Example 16 with MimeHeaders

use of javax.xml.soap.MimeHeaders in project OpenAM by OpenRock.

the class SOAPCommunicator method getHeaders.

/**
     * Returns mime headers in HTTP servlet request.
     *
     * @param req HTTP servlet request.
     * @return mime headers in HTTP servlet request.
     */
public MimeHeaders getHeaders(final HttpServletRequest req) {
    Enumeration<String> e = req.getHeaderNames();
    MimeHeaders headers = new MimeHeaders();
    while (e.hasMoreElements()) {
        String headerName = e.nextElement();
        String headerValue = req.getHeader(headerName);
        debug.message("SOAPCommunicator.getHeaders: Header name={}, value={}", headerName, headerValue);
        StringTokenizer values = new StringTokenizer(headerValue, ",");
        while (values.hasMoreTokens()) {
            headers.addHeader(headerName, values.nextToken().trim());
        }
    }
    if (debug.messageEnabled()) {
        debug.message("SOAPCommunicator.getHeaders: Header=" + headers.toString());
    }
    return headers;
}
Also used : MimeHeaders(javax.xml.soap.MimeHeaders) StringTokenizer(java.util.StringTokenizer)

Example 17 with MimeHeaders

use of javax.xml.soap.MimeHeaders in project OpenAM by OpenRock.

the class FSSSOAndFedService method doPost.

/**
     * Processes single sign on POST request.
     * @param request <code>HttpServletRequest</code> object
     * @param response <code>HttpServletResponse</code> object
     * @exception ServletException, IOException if an error occurred
     */
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    FSUtils.debug.message("FSSSOAndFedService.doPost: Called");
    if ((request == null) || (response == null)) {
        response.sendError(response.SC_INTERNAL_SERVER_ERROR, FSUtils.bundle.getString("nullInputParameter"));
        return;
    }
    if (FSUtils.needSetLBCookieAndRedirect(request, response, true)) {
        return;
    }
    // Check if it's an LECP request
    if (isLECPRequest(request)) {
        // TODO: assume auth framework will understand this param
        String useForward = (String) request.getAttribute(Constants.FORWARD_PARAM);
        if (useForward != null && useForward.equals(Constants.FORWARD_YES_VALUE)) {
            // this is a forward POST after authentication, need to
            // use GET instead of POST here
            FSUtils.debug.message("FSSSOAndFedService.doPost: LECP forward");
            this.doGet(request, response);
        } else {
            try {
                MimeHeaders mimeHeaders = SAMLUtils.getMimeHeaders(request);
                ServletInputStream sInputStream = request.getInputStream();
                SOAPMessage soapMessage = msgFactory.createMessage(mimeHeaders, sInputStream);
                this.onMessage(request, response, soapMessage);
            } catch (SOAPException se) {
                throw new ServletException(se);
            }
        }
        return;
    }
    // obtain AuthnRequest message
    String enocodedAuthnRequest = request.getParameter(IFSConstants.POST_AUTHN_REQUEST_PARAM);
    if (enocodedAuthnRequest == null) {
        doGet(request, response);
        return;
    }
    enocodedAuthnRequest = enocodedAuthnRequest.replace(' ', '\n');
    if (FSUtils.debug.messageEnabled()) {
        FSUtils.debug.message("FSSSOAndFedService.doPost: " + "BASE64 encoded AuthnRequest at the RECEIVER: " + enocodedAuthnRequest);
    }
    //decode and create FSAuthnRequest object
    FSAuthnRequest authnRequest = null;
    try {
        authnRequest = FSAuthnRequest.parseBASE64EncodedString(enocodedAuthnRequest);
        if (authnRequest == null) {
            FSUtils.debug.error("FSSSOAndFedService: " + FSUtils.bundle.getString("invalidAuthnRequest"));
            String[] data = { FSUtils.bundle.getString("invalidAuthnRequest") };
            LogUtil.error(Level.INFO, LogUtil.INVALID_AUTHN_REQUEST, data);
            response.sendError(response.SC_BAD_REQUEST, FSUtils.bundle.getString("invalidAuthnRequest"));
            return;
        } else {
            if (FSUtils.debug.messageEnabled()) {
                FSUtils.debug.message("FSSSOAndFedService: " + "AuthnRequest received:" + authnRequest.toXMLString());
            }
        }
    } catch (FSException e) {
        if (FSUtils.debug.messageEnabled()) {
            FSUtils.debug.message("FSSSOAndFedService: " + FSUtils.bundle.getString("invalidAuthnRequest"), e);
        }
        response.sendError(response.SC_BAD_REQUEST, FSUtils.bundle.getString("invalidAuthnRequest"));
        return;
    }
    String metaAlias = null;
    String realm = null;
    String hostEntityId = null;
    IDPDescriptorType hostedDesc = null;
    BaseConfigType hostedConfig = null;
    try {
        metaAlias = FSServiceUtils.getMetaAlias(request);
        realm = IDFFMetaUtils.getRealmByMetaAlias(metaAlias);
        hostEntityId = metaManager.getEntityIDByMetaAlias(metaAlias);
        hostedDesc = metaManager.getIDPDescriptor(realm, hostEntityId);
        hostedConfig = metaManager.getIDPDescriptorConfig(realm, hostEntityId);
    } catch (Exception e) {
        if (FSUtils.debug.messageEnabled()) {
            FSUtils.debug.message("FSSSOAndFedService: couldn't obtain hosted entity id:", e);
        }
    }
    handleAuthnRequest(request, response, authnRequest, false, false, realm, hostEntityId, metaAlias, hostedDesc, hostedConfig);
    return;
}
Also used : ServletException(javax.servlet.ServletException) IDPDescriptorType(com.sun.identity.liberty.ws.meta.jaxb.IDPDescriptorType) BaseConfigType(com.sun.identity.federation.jaxb.entityconfig.BaseConfigType) MimeHeaders(javax.xml.soap.MimeHeaders) ServletInputStream(javax.servlet.ServletInputStream) SOAPException(javax.xml.soap.SOAPException) FSAuthnRequest(com.sun.identity.federation.message.FSAuthnRequest) FSException(com.sun.identity.federation.common.FSException) SOAPMessage(javax.xml.soap.SOAPMessage) ServletException(javax.servlet.ServletException) SOAPException(javax.xml.soap.SOAPException) SessionException(com.sun.identity.plugin.session.SessionException) FSException(com.sun.identity.federation.common.FSException) IOException(java.io.IOException) FSAccountMgmtException(com.sun.identity.federation.accountmgmt.FSAccountMgmtException)

Example 18 with MimeHeaders

use of javax.xml.soap.MimeHeaders in project OpenAM by OpenRock.

the class SAMLSOAPReceiver method doPost.

/**
     * Processes request coming from SOAP.
     *
     * @param req <code>HttpServletRequest</code> object.
     * @param resp <code>HttpServletResponse</code> object.
     * @throws ServletException if there is an error.
     * @throws IOException if there is an error.
     */
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, java.io.IOException {
    if (SAMLUtils.getMaxContentLength() != 0) {
        int length = req.getContentLength();
        if (length == -1) {
            throw new ServletException(SAMLUtils.bundle.getString("unknownLength"));
        }
        if (length > SAMLUtils.getMaxContentLength()) {
            if (SAMLUtils.debug.messageEnabled()) {
                SAMLUtils.debug.message("content length too large" + length);
            }
            throw new ServletException(SAMLUtils.bundle.getString("largeContentLength"));
        }
    }
    String remoteAddr = ClientUtils.getClientIPAddress(req);
    Set partnerSourceID = null;
    if ((partnerSourceID = checkCaller(req, resp)) != null) {
        if (SAMLUtils.debug.messageEnabled()) {
            SAMLUtils.debug.message(" got request from a trusted server, " + "processing it now..");
        }
        try {
            MimeHeaders mimeHeaders = SAMLUtils.getMimeHeaders(req);
            ServletInputStream sInputStream = req.getInputStream();
            //Create the SOAPMessage from the reply
            SOAPMessage soapMessage = msgFactory.createMessage(mimeHeaders, sInputStream);
            SOAPMessage soapMessageReply = null;
            soapMessageReply = this.onMessage(req, resp, soapMessage, partnerSourceID);
            if (soapMessageReply != null) {
                if (soapMessageReply.saveRequired())
                    soapMessageReply.saveChanges();
                //Check to see if presence of SOAPFault
                if (containsFault(soapMessageReply)) {
                    if (SAMLUtils.debug.messageEnabled()) {
                        SAMLUtils.debug.message("Contains a SOAPFault!");
                    }
                    resp.setStatus(resp.SC_INTERNAL_SERVER_ERROR);
                } else {
                    resp.setStatus(resp.SC_OK);
                }
                //Send the response back to the senderby placing
                //the mime headers into the response, and
                //externalizing the soapmessage onto the response object
                SAMLUtils.setMimeHeaders(soapMessageReply.getMimeHeaders(), resp);
                ServletOutputStream sOutputStream = resp.getOutputStream();
                soapMessageReply.writeTo(sOutputStream);
                sOutputStream.flush();
            }
        } catch (Exception e) {
            throw new ServletException(e);
        }
    } else {
        // its not trusted site
        SAMLUtils.debug.error("Error message from SOAP Receiver:" + remoteAddr + " is untrusted site");
        String[] data = { SAMLUtils.bundle.getString("untrustedSite"), remoteAddr };
        LogUtils.error(java.util.logging.Level.INFO, LogUtils.UNTRUSTED_SITE, data);
        SOAPMessage faultReply = FormSOAPError(resp, "Server", "untrustedSite", null);
        SAMLUtils.setMimeHeaders(faultReply.getMimeHeaders(), resp);
        ServletOutputStream sOutputStream = resp.getOutputStream();
        try {
            faultReply.writeTo(sOutputStream);
        } catch (SOAPException se) {
            throw new ServletException(se);
        }
        sOutputStream.flush();
    }
}
Also used : ServletException(javax.servlet.ServletException) MimeHeaders(javax.xml.soap.MimeHeaders) Set(java.util.Set) HashSet(java.util.HashSet) ServletInputStream(javax.servlet.ServletInputStream) ServletOutputStream(javax.servlet.ServletOutputStream) SOAPException(javax.xml.soap.SOAPException) SOAPMessage(javax.xml.soap.SOAPMessage) ServletException(javax.servlet.ServletException) SOAPException(javax.xml.soap.SOAPException) SAMLRequestVersionTooHighException(com.sun.identity.saml.common.SAMLRequestVersionTooHighException) SAMLRequesterException(com.sun.identity.saml.common.SAMLRequesterException) SAMLRequestVersionTooLowException(com.sun.identity.saml.common.SAMLRequestVersionTooLowException) SAMLException(com.sun.identity.saml.common.SAMLException)

Example 19 with MimeHeaders

use of javax.xml.soap.MimeHeaders in project OpenAM by OpenRock.

the class SAMLUtils method getMimeHeaders.

/**
     * Returns a <code>MimeHeaders</code> object that contains the headers
     * in the given <code>HttpServletRequest</code> object.
     *
     * @param req the <code>HttpServletRequest</code> object.
     * @return a new <code>MimeHeaders</code> object containing the headers.
     */
public static MimeHeaders getMimeHeaders(HttpServletRequest req) {
    MimeHeaders headers = new MimeHeaders();
    if (req == null) {
        debug.message("SAMLUtils.getMimeHeaders: null input");
        return headers;
    }
    Enumeration enumerator = req.getHeaderNames();
    while (enumerator.hasMoreElements()) {
        String headerName = (String) enumerator.nextElement();
        String headerValue = req.getHeader(headerName);
        StringTokenizer values = new StringTokenizer(headerValue, ",");
        while (values.hasMoreTokens()) {
            headers.addHeader(headerName, values.nextToken().trim());
        }
    }
    return headers;
}
Also used : MimeHeaders(javax.xml.soap.MimeHeaders) StringTokenizer(java.util.StringTokenizer) Enumeration(java.util.Enumeration)

Example 20 with MimeHeaders

use of javax.xml.soap.MimeHeaders in project tdi-studio-se by Talend.

the class SOAPUtil method extractContentAsDocument.

/**
	 * invoke soap and return the response document
	 */
public String extractContentAsDocument(String version, String destination, String soapAction, String soapMessage) throws SOAPException, TransformerException, ParserConfigurationException, FileNotFoundException, UnsupportedEncodingException {
    MessageFactory messageFactory = null;
    if (version.equals(SOAP12)) {
        messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
    } else {
        messageFactory = MessageFactory.newInstance();
    }
    SOAPMessage message = messageFactory.createMessage();
    MimeHeaders mimeHeaders = message.getMimeHeaders();
    mimeHeaders.setHeader("SOAPAction", soapAction);
    SOAPPart soapPart = message.getSOAPPart();
    String encoding = getEncoding(soapMessage);
    ByteArrayInputStream stream = new ByteArrayInputStream(soapMessage.getBytes(encoding));
    StreamSource preppedMsgSrc = new StreamSource(stream);
    soapPart.setContent(preppedMsgSrc);
    message.saveChanges();
    SOAPMessage reply = connection.call(message, destination);
    SOAPPart reSoapPart = reply.getSOAPPart();
    if (reSoapPart != null && reSoapPart instanceof SOAPPartImpl) {
        ((SOAPPartImpl) reSoapPart).setSourceCharsetEncoding(encoding);
    }
    try {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        t.transform(reSoapPart.getContent(), new StreamResult(bos));
        return bos.toString(encoding);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
Also used : SOAPPartImpl(com.sun.xml.messaging.saaj.soap.SOAPPartImpl) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) MessageFactory(javax.xml.soap.MessageFactory) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SOAPMessage(javax.xml.soap.SOAPMessage) TransformerException(javax.xml.transform.TransformerException) SOAPException(javax.xml.soap.SOAPException) FileNotFoundException(java.io.FileNotFoundException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MimeHeaders(javax.xml.soap.MimeHeaders) ByteArrayInputStream(java.io.ByteArrayInputStream) SOAPPart(javax.xml.soap.SOAPPart)

Aggregations

MimeHeaders (javax.xml.soap.MimeHeaders)24 SOAPMessage (javax.xml.soap.SOAPMessage)16 SOAPException (javax.xml.soap.SOAPException)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 IOException (java.io.IOException)7 InputStream (java.io.InputStream)6 ServletException (javax.servlet.ServletException)5 MessageFactory (javax.xml.soap.MessageFactory)4 FSException (com.sun.identity.federation.common.FSException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Enumeration (java.util.Enumeration)3 StringTokenizer (java.util.StringTokenizer)3 ServletInputStream (javax.servlet.ServletInputStream)3 FSAccountMgmtException (com.sun.identity.federation.accountmgmt.FSAccountMgmtException)2 SessionException (com.sun.identity.plugin.session.SessionException)2 SAMLException (com.sun.identity.saml.common.SAMLException)2 SAMLRequestVersionTooHighException (com.sun.identity.saml.common.SAMLRequestVersionTooHighException)2 SAMLRequestVersionTooLowException (com.sun.identity.saml.common.SAMLRequestVersionTooLowException)2 SAMLRequesterException (com.sun.identity.saml.common.SAMLRequesterException)2 SOAPPartImpl (com.sun.xml.messaging.saaj.soap.SOAPPartImpl)2