Example usage for javax.xml.soap MimeHeaders MimeHeaders

List of usage examples for javax.xml.soap MimeHeaders MimeHeaders

Introduction

In this page you can find the example usage for javax.xml.soap MimeHeaders MimeHeaders.

Prototype

public MimeHeaders() 

Source Link

Document

Constructs a default MimeHeaders object initialized with an empty Vector object.

Usage

From source file:net.sf.sripathi.ws.mock.util.WebServiceutil.java

/**
 * Invokes the web service using SAAJ API.
 * // w  w  w .j  a  v a 2s.  c  om
 * @param request soap request string.
 * @param url end point URL.
 * @param user user name for authentication.
 * @param password password for authentication.
 * 
 * @return response soap string.
 */
public static String callWebService(String request, String url, String user, String password) {

    if (request == null)
        request = "";
    try {
        SOAPConnection conn = SOAPConnectionFactory.newInstance().createConnection();

        MimeHeaders hd = new MimeHeaders();
        hd.addHeader("Content-Type", "text/xml");
        if (StringUtil.isValid(user) && StringUtil.isValid(password)) {
            String authorization = new String(Base64.encodeBase64((user + ":" + password).getBytes()));
            hd.addHeader("Authorization", "Basic " + authorization);
        }

        SOAPMessage msg = MessageFactory.newInstance().createMessage(hd,
                new ByteArrayInputStream(request.getBytes()));
        SOAPMessage resp = conn.call(msg, url);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        resp.writeTo(baos);
        return new String(baos.toByteArray());
    } catch (Exception e) {
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        return sw.toString();
    }
}

From source file:com.googlecode.ddom.frontend.saaj.impl.SwAProfile.java

static MimeHeaders readMimeHeaders(MultipartReader mpr) throws IOException {
    MimeHeaders headers = new MimeHeaders();
    while (mpr.nextHeader()) {
        headers.addHeader(mpr.getHeaderName(), mpr.getHeaderValue());
    }// w w  w. jav  a2s  .com
    return headers;
}

From source file:com.wandrell.example.swss.test.util.factory.SecureSoapMessages.java

/**
 * Creates a SOAP message with a digested password, username and nonce.
 * <p>//from  w  w w  .ja  va  2s  . c  om
 * The nonce will be generated during the securing process.
 * <p>
 * A freemarker template should be provided, it will be used to generate the
 * final message from the received parameters.
 *
 * @param path
 *            path to the freemarker template
 * @param user
 *            user to include in the message
 * @param password
 *            password to include in the message
 * @return a digested password {@code SOAPMessage}
 * @return a SOAP message with a digested password, username and nonce
 * @throws Exception
 *             if any error occurs during the message creation
 */
public static final SOAPMessage getDigestedPasswordMessage(final String path, final String user,
        final String password) throws Exception {
    return MessageFactory.newInstance().createMessage(new MimeHeaders(),
            getDigestedPasswordStream(path, user, password));
}

From source file:eu.europeana.uim.sugarcrmclient.internal.ExtendedSaajSoapMessageFactory.java

private MimeHeaders parseMimeHeaders(InputStream inputStream) throws IOException {
    MimeHeaders mimeHeaders = new MimeHeaders();
    if (inputStream instanceof TransportInputStream) {
        TransportInputStream transportInputStream = (TransportInputStream) inputStream;
        for (Iterator headerNames = transportInputStream.getHeaderNames(); headerNames.hasNext();) {
            String headerName = (String) headerNames.next();
            for (Iterator headerValues = transportInputStream.getHeaders(headerName); headerValues.hasNext();) {
                String headerValue = (String) headerValues.next();
                StringTokenizer tokenizer = new StringTokenizer(headerValue, ",");
                while (tokenizer.hasMoreTokens()) {
                    mimeHeaders.addHeader(headerName, tokenizer.nextToken().trim());
                }/*from  www . ja v  a 2 s  .co  m*/
            }
        }
    }
    return mimeHeaders;
}

From source file:com.googlecode.ddom.saaj.SOAPPartTest.java

/**
 * Tests the behavior of {@link SOAPPart#getContent()} for a plain SOAP message created from an
 * input stream.//ww  w.j a  v a  2  s.  c  om
 * 
 * @throws Exception
 * 
 * @see SOAPMessageTest#testWriteTo()
 */
@Validated
@Test
public void testGetContent() throws Exception {
    MimeHeaders headers = new MimeHeaders();
    headers.addHeader("Content-Type", "text/xml; charset=utf-8");
    InputStream in = MessageSet.SOAP11.getTestMessage("message.xml");
    byte[] orgContent = IOUtils.toByteArray(in);
    SOAPMessage message = factory.createMessage(headers, new ByteArrayInputStream(orgContent));

    // Get the content before accessing the SOAP part
    Source source1 = message.getSOAPPart().getContent();
    assertTrue(source1 instanceof StreamSource);
    byte[] content1 = IOUtils.toByteArray(((StreamSource) source1).getInputStream());
    assertTrue(Arrays.equals(orgContent, content1));

    // Now access the SOAP part and get the content again
    message.getSOAPPart().getEnvelope();
    // Note that the fact that we can still access the SOAP part (although we have consumed
    // the input stream returned by getContent) means that the SAAJ implementation has
    // copied the content of the stream.
    Source source2 = message.getSOAPPart().getContent();
    // The source is now a DOMSource. 
    assertTrue(source2 instanceof DOMSource);
}

From source file:py.una.pol.karaku.test.test.services.KarakuFaultMessageResolverTest.java

/**
 * @return/*from  www .  j a va 2 s  . com*/
 * @throws OPException
 * @throws Exception
 */
private SOAPMessage getFromString(String xml) throws Exception {

    MessageFactory factory = MessageFactory.newInstance();
    SOAPMessage message = factory.createMessage(new MimeHeaders(),
            new ByteArrayInputStream(xml.getBytes(Charset.forName("UTF-8"))));
    return message;
}

From source file:com.inbravo.scribe.rest.service.crm.ms.auth.SOAPExecutor.java

/**
 * Executes SOAP message//from www .ja v  a 2s.co  m
 * 
 * @param endpointUrl SOAP endpoint
 * @param request SOAP request
 * @return SOAP response message
 * @throws SOAPException in case of a SOAP issue
 * @throws IOException in case of an IO issue
 */
public final SOAPMessage execute(final String endpointUrl, final String request) throws Exception {

    if (logger.isDebugEnabled()) {
        logger.debug("----Inside execute endpointUrl: " + endpointUrl + " & request: " + request);
    }

    /* Create SOAP message */
    final SOAPMessage message = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL)
            .createMessage(new MimeHeaders(), new ByteArrayInputStream(request.getBytes()));

    /* Execute the url */
    return this.execute(endpointUrl, message);
}

From source file:com.googlecode.ddom.saaj.SOAPMessageTest.java

@Validated
@Test//from www  .  j ava2 s. c  o m
public void testGetAttachmentsFiltered() throws Exception {
    SOAPMessage message = getFactory().createMessage();

    AttachmentPart att1 = message.createAttachmentPart();
    att1.addMimeHeader("Content-Type", "text/plain");
    message.addAttachmentPart(att1);

    AttachmentPart att2 = message.createAttachmentPart();
    att2.addMimeHeader("Content-Type", "application/octet-stream");
    message.addAttachmentPart(att2);

    AttachmentPart att3 = message.createAttachmentPart();
    att3.addMimeHeader("Content-ID", "<123456@example.com>");
    att3.addMimeHeader("Content-Type", "text/plain");
    message.addAttachmentPart(att3);

    MimeHeaders headers = new MimeHeaders();
    headers.addHeader("Content-Type", "text/plain");
    Iterator it = message.getAttachments(headers);
    assertTrue(it.hasNext());
    assertSame(att1, it.next());
    assertTrue(it.hasNext());
    assertSame(att3, it.next());
    assertFalse(it.hasNext());
}

From source file:com.wandrell.example.swss.test.util.factory.SecureSoapMessages.java

/**
 * Creates a SOAP message with a plain password and username.
 * <p>/*from  www  .  j a v  a2  s  . c o m*/
 * A freemarker template should be provided, it will be used to generate the
 * final message from the received parameters.
 * 
 * @param path
 *            path to the freemarker template
 * @param user
 *            user to include in the message
 * @param password
 *            password to include in the message
 * @return a SOAP message with a plain password and username
 * @throws Exception
 *             if any error occurs during the message creation
 */
public static final SOAPMessage getPlainPasswordMessage(final String path, final String user,
        final String password) throws Exception {
    return MessageFactory.newInstance().createMessage(new MimeHeaders(),
            getPlainPasswordStream(path, user, password));
}

From source file:com.googlecode.ddom.saaj.SOAPMessageTest.java

/**
 * Tests the behavior of {@link SOAPPart#getContent()} for a plain SOAP message created from an
 * input stream.//  w ww.ja  v  a  2 s. c  o m
 * 
 * @throws Exception
 * 
 * @see SOAPPartTest#testGetContent()
 */
@Validated
@Test
public void testWriteTo() throws Exception {
    MimeHeaders headers = new MimeHeaders();
    headers.addHeader("Content-Type", messageSet.getVersion().getContentType() + "; charset=utf-8");
    InputStream in = messageSet.getTestMessage("message.xml");
    byte[] orgContent = IOUtils.toByteArray(in);
    SOAPMessage message = getFactory().createMessage(headers, new ByteArrayInputStream(orgContent));

    // Get the content before accessing the SOAP part
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    message.writeTo(baos);
    byte[] content1 = baos.toByteArray();
    assertTrue(Arrays.equals(orgContent, content1));

    // Now access the SOAP part and get the content again
    message.getSOAPPart().getEnvelope();
    baos = new ByteArrayOutputStream();
    message.writeTo(baos);
    byte[] content2 = baos.toByteArray();
    // The content is equivalent, but not exactly the same
    assertFalse(Arrays.equals(orgContent, content2));
}