Search in sources :

Example 6 with MimeHeaders

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

the class IDPArtifactResolution method doArtifactResolution.

/**
     * This method processes the artifact resolution request coming 
     * from a service provider. It processes the artifact
     * resolution request sent by the service provider and 
     * sends back a proper SOAPMessage that contains an Assertion.
     *
     * @param request the <code>HttpServletRequest</code> object
     * @param response the <code>HttpServletResponse</code> object
     */
public static void doArtifactResolution(HttpServletRequest request, HttpServletResponse response) {
    String classMethod = "IDPArtifactResolution.doArtifactResolution: ";
    try {
        String idpMetaAlias = request.getParameter(SAML2MetaManager.NAME_META_ALIAS_IN_URI);
        if ((idpMetaAlias == null) || (idpMetaAlias.trim().length() == 0)) {
            idpMetaAlias = SAML2MetaUtils.getMetaAliasByUri(request.getRequestURI());
        }
        if ((idpMetaAlias == null) || (idpMetaAlias.trim().length() == 0)) {
            if (SAML2Utils.debug.messageEnabled()) {
                SAML2Utils.debug.message(classMethod + "unable to get IDP meta alias from request.");
            }
            String[] data = { idpMetaAlias };
            LogUtil.error(Level.INFO, LogUtil.IDP_METADATA_ERROR, data, null);
            SAMLUtils.sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "nullIDPMetaAlias", SAML2Utils.bundle.getString("nullIDPMetaAlias"));
            return;
        }
        // retrieve IDP entity id from meta alias
        String idpEntityID = null;
        String realm = null;
        try {
            idpEntityID = IDPSSOUtil.metaManager.getEntityByMetaAlias(idpMetaAlias);
            if ((idpEntityID == null) || (idpEntityID.trim().length() == 0)) {
                SAMLUtils.debug.error(classMethod + "Unable to get IDP Entity ID from meta.");
                String[] data = { idpEntityID };
                LogUtil.error(Level.INFO, LogUtil.INVALID_IDP, data, null);
                SAMLUtils.sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "nullIDPEntityID", SAML2Utils.bundle.getString("nullIDPEntityID"));
                return;
            }
            realm = SAML2MetaUtils.getRealmByMetaAlias(idpMetaAlias);
        } catch (SAML2MetaException sme) {
            SAML2Utils.debug.error(classMethod + "Unable to get IDP Entity ID from meta.");
            String[] data = { idpMetaAlias };
            LogUtil.error(Level.INFO, LogUtil.IDP_METADATA_ERROR, data, null);
            SAMLUtils.sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "metaDataError", SAML2Utils.bundle.getString("metaDataError"));
            return;
        }
        if (!SAML2Utils.isIDPProfileBindingSupported(realm, idpEntityID, SAML2Constants.ARTIFACT_RESOLUTION_SERVICE, SAML2Constants.SOAP)) {
            SAML2Utils.debug.error(classMethod + "Artifact Resolution Service binding: Redirect is not " + "supported for " + idpEntityID);
            String[] data = { idpEntityID, SAML2Constants.SOAP };
            LogUtil.error(Level.INFO, LogUtil.BINDING_NOT_SUPPORTED, data, null);
            SAMLUtils.sendError(request, response, HttpServletResponse.SC_BAD_REQUEST, "unsupportedBinding", SAML2Utils.bundle.getString("unsupportedBinding"));
            return;
        }
        try {
            // Get all the headers from the HTTP request
            MimeHeaders headers = getHeaders(request);
            // Get the body of the HTTP request
            InputStream is = request.getInputStream();
            SOAPMessage msg = messageFactory.createMessage(headers, is);
            SOAPMessage reply = null;
            reply = onMessage(msg, request, response, realm, idpEntityID);
            if (reply != null) {
                /* Need to call saveChanges because we're
                     * going to use the MimeHeaders to set HTTP
                     * response information. These MimeHeaders
                     * are generated as part of the save. */
                if (reply.saveRequired()) {
                    reply.saveChanges();
                }
                response.setStatus(HttpServletResponse.SC_OK);
                putHeaders(reply.getMimeHeaders(), response);
                // Write out the message on the response stream
                OutputStream outputStream = response.getOutputStream();
                reply.writeTo(outputStream);
                outputStream.flush();
            } else {
                response.setStatus(HttpServletResponse.SC_NO_CONTENT);
            }
        } catch (SOAPException ex) {
            SAML2Utils.debug.error(classMethod + "SOAP error", ex);
            String[] data = { idpEntityID };
            LogUtil.error(Level.INFO, LogUtil.INVALID_SOAP_MESSAGE, data, null);
            SAMLUtils.sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "invalidSOAPMessage", SAML2Utils.bundle.getString("invalidSOAPMessage") + " " + ex.getMessage());
            return;
        } catch (SAML2Exception se) {
            SAML2Utils.debug.error(classMethod + "SAML2 error", se);
            SAMLUtils.sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "unableToCreateArtifactResponse", SAML2Utils.bundle.getString("unableToCreateArtifactResponse") + " " + se.getMessage());
            return;
        }
    } catch (IOException ioe) {
        SAML2Utils.debug.error(classMethod + "I/O rrror", ioe);
    }
}
Also used : SAML2Exception(com.sun.identity.saml2.common.SAML2Exception) MimeHeaders(javax.xml.soap.MimeHeaders) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) SOAPException(javax.xml.soap.SOAPException) IOException(java.io.IOException) SAML2MetaException(com.sun.identity.saml2.meta.SAML2MetaException) SOAPMessage(javax.xml.soap.SOAPMessage)

Example 7 with MimeHeaders

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

the class IDPArtifactResolution method getHeaders.

// gets the MIME headers from a HTTPRequest
private static MimeHeaders getHeaders(HttpServletRequest req) {
    Enumeration enumerator = req.getHeaderNames();
    MimeHeaders headers = new MimeHeaders();
    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 8 with MimeHeaders

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

the class SOAPCommunicator method createSOAPMessage.

/**
     * Creates <code>SOAPMessage</code> with the input XML String
     * as message header and body.
     *
     * @param header          XML string to be put into <code>SOAPMessage</code> header.
     * @param body            XML string to be put into <code>SOAPMessage</code> body.
     * @param isClientMessage true if the message is sent from SOAP client to
     *                        server.
     * @return newly created <code>SOAPMessage</code>.
     * @throws SOAPException if it cannot create the <code>SOAPMessage</code>.
     */
public SOAPMessage createSOAPMessage(final String header, final String body, final boolean isClientMessage) throws SOAPException, SAML2Exception {
    try {
        MimeHeaders mimeHeaders = new MimeHeaders();
        mimeHeaders.addHeader("Content-Type", "text/xml");
        if (isClientMessage) {
            mimeHeaders.addHeader("SOAPAction", "\"\"");
        }
        if (debug.messageEnabled()) {
            debug.message("SOAPCommunicator.createSOAPMessage: header = " + header + ", body = " + body);
        }
        StringBuilder sb = new StringBuilder(500);
        sb.append("<").append(SAMLConstants.SOAP_ENV_PREFIX).append(":Envelope").append(SAMLConstants.SPACE).append("xmlns:").append(SAMLConstants.SOAP_ENV_PREFIX).append("=\"").append(SAMLConstants.SOAP_URI).append("\">");
        if (header != null) {
            sb.append("<").append(SAMLConstants.SOAP_ENV_PREFIX).append(":Header>").append(header).append(SAMLConstants.START_END_ELEMENT).append(SAMLConstants.SOAP_ENV_PREFIX).append(":Header>");
        }
        if (body != null) {
            sb.append("<").append(SAMLConstants.SOAP_ENV_PREFIX).append(":Body>").append(body).append(SAMLConstants.START_END_ELEMENT).append(SAMLConstants.SOAP_ENV_PREFIX).append(":Body>");
        }
        sb.append(SAMLConstants.START_END_ELEMENT).append(SAMLConstants.SOAP_ENV_PREFIX).append(":Envelope>").append(SAMLConstants.NL);
        if (debug.messageEnabled()) {
            debug.message("SOAPCommunicator.createSOAPMessage: soap message = " + sb.toString());
        }
        return messageFactory.createMessage(mimeHeaders, new ByteArrayInputStream(sb.toString().getBytes(SAML2Constants.DEFAULT_ENCODING)));
    } catch (IOException io) {
        debug.error("SOAPCommunicator.createSOAPMessage: IOE", io);
        throw new SAML2Exception(io.getMessage());
    }
}
Also used : MimeHeaders(javax.xml.soap.MimeHeaders) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException)

Example 9 with MimeHeaders

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

the class SAMLSOAPReceiver method FormMessageResponse.

/**
     * This message forms the SAML Response and puts it in the 
     * SOAPMessage's Body.
     */
private SOAPMessage FormMessageResponse(HttpServletResponse servletResp, Response resp) {
    SOAPMessage msg = null;
    MimeHeaders mimeHeaders = new MimeHeaders();
    mimeHeaders.addHeader("Content-Type", "text/xml");
    StringBuffer envBegin = new StringBuffer(100);
    envBegin.append("<").append(sc.SOAP_ENV_PREFIX).append(":Envelope").append(sc.SPACE).append("xmlns:").append(sc.SOAP_ENV_PREFIX).append("=\"").append(sc.SOAP_URI).append("\">").append(sc.NL);
    envBegin.append("<").append(sc.SOAP_ENV_PREFIX).append(":Body>").append(sc.NL);
    StringBuffer envEnd = new StringBuffer(100);
    envEnd.append(sc.START_END_ELEMENT).append(sc.SOAP_ENV_PREFIX).append(":Body>").append(sc.NL);
    envEnd.append(sc.START_END_ELEMENT).append(sc.SOAP_ENV_PREFIX).append(":Envelope>").append(sc.NL);
    try {
        StringBuffer sb = new StringBuffer(300);
        sb.append(envBegin).append(resp.toString()).append(envEnd);
        if (SAMLUtils.debug.messageEnabled()) {
            SAMLUtils.debug.message("response created is: " + sb.toString());
        }
        ByteArrayOutputStream bop = new ByteArrayOutputStream();
        bop.write(sb.toString().getBytes(sc.DEFAULT_ENCODING));
        msg = msgFactory.createMessage(mimeHeaders, new ByteArrayInputStream(bop.toByteArray()));
    } catch (Exception e) {
        SAMLUtils.debug.error("could not build response:" + e.getMessage());
        servletResp.setStatus(servletResp.SC_INTERNAL_SERVER_ERROR);
        return FormSOAPError(servletResp, "Server", "cannotBuildResponse", "cannotVerifyIdentity");
    }
    return msg;
}
Also used : MimeHeaders(javax.xml.soap.MimeHeaders) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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 10 with MimeHeaders

use of javax.xml.soap.MimeHeaders in project webservices-axiom by apache.

the class TestExamineMustUnderstandHeaderElements method runTest.

@Override
protected void runTest() throws Throwable {
    MessageFactory messageFactory = spec.getAdapter(FactorySelector.class).newMessageFactory(saajImplementation, dynamic);
    MimeHeaders mimeHeaders = new MimeHeaders();
    mimeHeaders.addHeader("Content-Type", spec.getContentType());
    InputStream in = SOAPSampleSet.MUST_UNDERSTAND.getMessage(spec).getInputStream();
    try {
        SOAPMessage message = messageFactory.createMessage(mimeHeaders, in);
        SOAPHeader header = message.getSOAPHeader();
        Iterator<?> it = header.examineMustUnderstandHeaderElements(null);
        assertTrue(it.hasNext());
        assertTrue(it.next() instanceof SOAPHeaderElement);
        assertFalse(it.hasNext());
    } finally {
        in.close();
    }
}
Also used : FactorySelector(org.apache.axiom.ts.saaj.FactorySelector) SOAPHeaderElement(javax.xml.soap.SOAPHeaderElement) MimeHeaders(javax.xml.soap.MimeHeaders) MessageFactory(javax.xml.soap.MessageFactory) InputStream(java.io.InputStream) SOAPMessage(javax.xml.soap.SOAPMessage) SOAPHeader(javax.xml.soap.SOAPHeader)

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