01: /*
02: * soapUI, copyright (C) 2004-2007 eviware.com
03: *
04: * soapUI is free software; you can redistribute it and/or modify it under the
05: * terms of version 2.1 of the GNU Lesser General Public License as published by
06: * the Free Software Foundation.
07: *
08: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
09: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: * See the GNU Lesser General Public License for more details at gnu.org.
11: */
12:
13: package com.eviware.soapui.impl.wsdl.submit.transports.http;
14:
15: import java.io.ByteArrayOutputStream;
16: import java.io.IOException;
17: import java.io.OutputStream;
18:
19: import javax.mail.MessagingException;
20: import javax.mail.internet.MimeMessage;
21: import javax.mail.internet.MimeMultipart;
22:
23: import org.apache.commons.httpclient.methods.RequestEntity;
24:
25: import com.eviware.soapui.SoapUI;
26: import com.eviware.soapui.impl.wsdl.WsdlInterface;
27: import com.eviware.soapui.impl.wsdl.WsdlRequest;
28:
29: /**
30: * MimeMessage request class
31: *
32: * @author ole.matzura
33: */
34:
35: public class MimeMessageRequestEntity implements RequestEntity {
36: private final MimeMessage message;
37: private byte[] buffer;
38: private final boolean isXOP;
39: private final WsdlRequest wsdlRequest;
40:
41: public MimeMessageRequestEntity(MimeMessage message, boolean isXOP,
42: WsdlRequest wsdlRequest) {
43: this .message = message;
44: this .isXOP = isXOP;
45: this .wsdlRequest = wsdlRequest;
46: }
47:
48: public long getContentLength() {
49: try {
50: ByteArrayOutputStream out = new ByteArrayOutputStream();
51: writeRequest(out);
52: buffer = out.toByteArray();
53: return buffer.length;
54: } catch (Exception e) {
55: SoapUI.logError(e);
56: return -1;
57: }
58: }
59:
60: public String getContentType() {
61: try {
62: if (isXOP) {
63: String header = message.getHeader("Content-Type")[0];
64: return AttachmentUtils.buildMTOMContentType(header,
65: wsdlRequest.getOperation().getAction(),
66: ((WsdlInterface) wsdlRequest.getOperation()
67: .getInterface()).getSoapVersion());
68: } else {
69: String header = message.getHeader("Content-Type")[0];
70: int ix = header.indexOf("boundary");
71: return "multipart/related; type=\"text/xml\"; start=\""
72: + AttachmentUtils.ROOTPART_SOAPUI_ORG + "\"; "
73: + header.substring(ix);
74: }
75: } catch (MessagingException e) {
76: SoapUI.logError(e);
77: }
78:
79: return null;
80: }
81:
82: public boolean isRepeatable() {
83: return true;
84: }
85:
86: public void writeRequest(OutputStream arg0) throws IOException {
87: try {
88: if (buffer == null) {
89: arg0.write("\r\n".getBytes());
90: ((MimeMultipart) message.getContent()).writeTo(arg0);
91: } else
92: arg0.write(buffer);
93: } catch (Exception e) {
94: SoapUI.logError(e);
95: }
96: }
97: }
|