Java tutorial
/* * soapui, copyright (C) 2006 eviware.com * * SoapUI is free software; you can redistribute it and/or modify it under the * terms of the GNU Lesser General Public License as published by the Free Software Foundation; * either version 2.1 of the License, or (at your option) any later version. * * SoapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details at gnu.org. */ package com.eviware.soapui.impl.wsdl.submit.transports.http; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import javax.mail.BodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HeaderElement; import org.apache.commons.httpclient.NameValuePair; import com.eviware.soapui.impl.wsdl.WsdlRequest; import com.eviware.soapui.model.iface.Attachment; import com.eviware.soapui.model.iface.Request; import com.eviware.soapui.settings.HttpSettings; import com.eviware.soapui.settings.WsdlSettings; import com.eviware.soapui.support.types.StringToStringMap; import com.eviware.soapui.support.xml.XmlUtils; public class MimeMessageResponse implements WsdlResponse { private final WsdlRequest wsdlRequest; private long timeTaken; private final List<Attachment> result = new ArrayList<Attachment>(); private Attachment rootPart; private MimeMessage message; private long responseContentLength; private StringToStringMap requestHeaders; private StringToStringMap responseHeaders; private final String requestContent; private String responseContent; public MimeMessageResponse(WsdlRequest wsdlRequest, final TimeablePostMethod postMethod, String requestContent) { this.wsdlRequest = wsdlRequest; this.requestContent = requestContent; this.timeTaken = postMethod.getTimeTaken(); responseContentLength = postMethod.getResponseContentLength(); try { initHeaders(postMethod); MimeMultipart mp = new MimeMultipart(new PostResponseDataSource(postMethod)); message = new MimeMessage(HttpClientRequestTransport.JAVAMAIL_SESSION); message.setContent(mp); Header h = postMethod.getResponseHeader("Content-Type"); HeaderElement[] elements = h.getElements(); String rootPartId = null; for (HeaderElement element : elements) { if (element.getName().toUpperCase().startsWith("MULTIPART/")) { NameValuePair parameter = element.getParameterByName("start"); if (parameter != null) rootPartId = parameter.getValue(); } } for (int c = 0; c < mp.getCount(); c++) { BodyPart bodyPart = mp.getBodyPart(c); if (bodyPart.getContentType().toUpperCase().startsWith("MULTIPART/")) { MimeMultipart mp2 = new MimeMultipart(new BodyPartDataSource(bodyPart)); for (int i = 0; i < mp2.getCount(); i++) { result.add(new BodyPartAttachment(mp2.getBodyPart(i))); } } else { BodyPartAttachment attachment = new BodyPartAttachment(bodyPart); String[] contentIdHeaders = bodyPart.getHeader("Content-ID"); if (contentIdHeaders != null && contentIdHeaders.length > 0 && contentIdHeaders[0].equals(rootPartId)) { rootPart = attachment; } else result.add(attachment); } } // if no explicit root part has been set, use the first one in the result if (rootPart == null) rootPart = result.remove(0); if (wsdlRequest.getSettings().getBoolean(HttpSettings.INCLUDE_RESPONSE_IN_TIME_TAKEN)) this.timeTaken = postMethod.getTimeTakenUntilNow(); } catch (Exception e) { e.printStackTrace(); } } public Attachment[] getAttachments() { return result.toArray(new Attachment[result.size()]); } public Attachment[] getAttachmentsForPart(String partName) { List<Attachment> results = new ArrayList<Attachment>(); for (Attachment attachment : result) { if (attachment.getPart().equals(partName)) results.add(attachment); } return results.toArray(new Attachment[results.size()]); } public String getContentAsString() { if (rootPart == null) return null; if (responseContent == null) { try { InputStream in = rootPart.getInputStream(); StringBuffer buf = new StringBuffer(); while (in.available() > 0) buf.append((char) in.read()); responseContent = buf.toString(); if (wsdlRequest.getSettings().getBoolean(WsdlSettings.PRETTY_PRINT_RESPONSE_MESSAGES)) { responseContent = XmlUtils.prettyPrintXml(responseContent); } return responseContent; } catch (Exception e) { e.printStackTrace(); } } return responseContent; } public long getContentLength() { return responseContentLength; } public Request getRequest() { return wsdlRequest; } public long getTimeTaken() { return timeTaken; } private void initHeaders(TimeablePostMethod postMethod) { requestHeaders = new StringToStringMap(); Header[] headers = postMethod.getRequestHeaders(); for (Header header : headers) { requestHeaders.put(header.getName(), header.getValue()); } responseHeaders = new StringToStringMap(); headers = postMethod.getResponseHeaders(); for (Header header : headers) { responseHeaders.put(header.getName(), header.getValue()); } responseHeaders.put("#status#", postMethod.getStatusLine().toString()); } public StringToStringMap getRequestHeaders() { return requestHeaders; } public StringToStringMap getResponseHeaders() { return responseHeaders; } public String getRequestContent() { return requestContent; } public void setResponseContent(String responseContent) { this.responseContent = responseContent; } }