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.mock.WsdlMockResponse;
28:
29: /**
30: * MimeMessage response for a WsdlMockResponse
31: *
32: * @author ole.matzura
33: */
34:
35: public class MimeMessageMockResponseEntity implements RequestEntity {
36: private final MimeMessage message;
37: private byte[] buffer;
38: private final boolean isXOP;
39: private final WsdlMockResponse wsdlRequest;
40:
41: public MimeMessageMockResponseEntity(MimeMessage message,
42: boolean isXOP, WsdlMockResponse response) {
43: this .message = message;
44: this .isXOP = isXOP;
45: this .wsdlRequest = response;
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.getMockOperation().getOperation()
66: .getAction(),
67: ((WsdlInterface) wsdlRequest.getMockOperation()
68: .getOperation().getInterface())
69: .getSoapVersion());
70: } else {
71: String header = message.getHeader("Content-Type")[0];
72: int ix = header.indexOf("boundary");
73: return "multipart/related; type=\"text/xml\"; start=\""
74: + AttachmentUtils.ROOTPART_SOAPUI_ORG + "\"; "
75: + header.substring(ix);
76: }
77: } catch (MessagingException e) {
78: SoapUI.logError(e);
79: }
80:
81: return null;
82: }
83:
84: public boolean isRepeatable() {
85: return true;
86: }
87:
88: public void writeRequest(OutputStream arg0) throws IOException {
89: try {
90: if (buffer == null) {
91: arg0.write("\r\n".getBytes());
92: ((MimeMultipart) message.getContent()).writeTo(arg0);
93: } else
94: arg0.write(buffer);
95: } catch (Exception e) {
96: SoapUI.logError(e);
97: }
98: }
99: }
|