Source Code Cross Referenced for WsdlMockResponse.java in  » Web-Services » soapui-1.7.5 » com » eviware » soapui » impl » wsdl » mock » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Services » soapui 1.7.5 » com.eviware.soapui.impl.wsdl.mock 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  soapUI, copyright (C) 2004-2007 eviware.com 
003:         *
004:         *  soapUI is free software; you can redistribute it and/or modify it under the 
005:         *  terms of version 2.1 of the GNU Lesser General Public License as published by 
006:         *  the Free Software Foundation.
007:         *
008:         *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
009:         *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
010:         *  See the GNU Lesser General Public License for more details at gnu.org.
011:         */
012:
013:        package com.eviware.soapui.impl.wsdl.mock;
014:
015:        import java.io.File;
016:        import java.io.IOException;
017:        import java.util.ArrayList;
018:        import java.util.Arrays;
019:        import java.util.List;
020:        import java.util.zip.GZIPOutputStream;
021:
022:        import javax.activation.DataHandler;
023:        import javax.mail.MessagingException;
024:        import javax.mail.internet.MimeBodyPart;
025:        import javax.mail.internet.MimeMessage;
026:        import javax.mail.internet.MimeMultipart;
027:        import javax.mail.internet.PreencodedMimeBodyPart;
028:        import javax.swing.ImageIcon;
029:        import javax.wsdl.BindingOperation;
030:        import javax.wsdl.Message;
031:
032:        import org.apache.log4j.Logger;
033:        import org.apache.xmlbeans.SchemaType;
034:
035:        import com.eviware.soapui.SoapUI;
036:        import com.eviware.soapui.config.AttachmentConfig;
037:        import com.eviware.soapui.config.HeaderConfig;
038:        import com.eviware.soapui.config.MockResponseConfig;
039:        import com.eviware.soapui.impl.wsdl.AbstractWsdlModelItem;
040:        import com.eviware.soapui.impl.wsdl.AttachmentContainer;
041:        import com.eviware.soapui.impl.wsdl.WsdlAttachmentPart;
042:        import com.eviware.soapui.impl.wsdl.WsdlContentPart;
043:        import com.eviware.soapui.impl.wsdl.WsdlHeaderPart;
044:        import com.eviware.soapui.impl.wsdl.WsdlOperation;
045:        import com.eviware.soapui.impl.wsdl.WsdlSubmitContext;
046:        import com.eviware.soapui.impl.wsdl.submit.filters.PropertyExpansionRequestFilter;
047:        import com.eviware.soapui.impl.wsdl.submit.filters.RemoveEmptyContentRequestFilter;
048:        import com.eviware.soapui.impl.wsdl.submit.transports.http.AttachmentUtils;
049:        import com.eviware.soapui.impl.wsdl.submit.transports.http.BodyPartAttachment;
050:        import com.eviware.soapui.impl.wsdl.submit.transports.http.MimeMessageMockResponseEntity;
051:        import com.eviware.soapui.impl.wsdl.submit.transports.http.MockResponseDataSource;
052:        import com.eviware.soapui.impl.wsdl.support.CompressedStringSupport;
053:        import com.eviware.soapui.impl.wsdl.support.FileAttachment;
054:        import com.eviware.soapui.impl.wsdl.support.MessageXmlObject;
055:        import com.eviware.soapui.impl.wsdl.support.MessageXmlPart;
056:        import com.eviware.soapui.impl.wsdl.support.MockFileAttachment;
057:        import com.eviware.soapui.impl.wsdl.support.ModelItemIconAnimator;
058:        import com.eviware.soapui.impl.wsdl.support.WsdlAttachment;
059:        import com.eviware.soapui.impl.wsdl.support.soap.SoapVersion;
060:        import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlContext;
061:        import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlUtils;
062:        import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlUtils.SoapHeader;
063:        import com.eviware.soapui.model.iface.Attachment;
064:        import com.eviware.soapui.model.iface.MessagePart;
065:        import com.eviware.soapui.model.mock.MockResponse;
066:        import com.eviware.soapui.model.mock.MockRunContext;
067:        import com.eviware.soapui.settings.WsdlSettings;
068:        import com.eviware.soapui.support.Tools;
069:        import com.eviware.soapui.support.UISupport;
070:        import com.eviware.soapui.support.scripting.ScriptEnginePool;
071:        import com.eviware.soapui.support.scripting.SoapUIScriptEngine;
072:        import com.eviware.soapui.support.types.StringToStringMap;
073:        import com.eviware.soapui.support.xml.XmlUtils;
074:
075:        /**
076:         * A WsdlMockResponse contained by a WsdlMockOperation
077:         * 
078:         * @author ole.matzura
079:         */
080:
081:        public class WsdlMockResponse extends
082:                AbstractWsdlModelItem<MockResponseConfig> implements 
083:                MockResponse, AttachmentContainer {
084:            private final static Logger log = Logger
085:                    .getLogger(WsdlMockResponse.class);
086:
087:            public final static String MOCKRESULT_PROPERTY = WsdlMockResponse.class
088:                    .getName()
089:                    + "@mockresult";
090:            public final static String SCRIPT_PROPERTY = WsdlMockResponse.class
091:                    .getName()
092:                    + "@script";
093:            public final static String HEADERS_PROPERTY = WsdlMockResponse.class
094:                    .getName()
095:                    + "@headers";
096:            public final static String DISABLE_MULTIPART_ATTACHMENTS = WsdlMockResponse.class
097:                    .getName()
098:                    + "@disable-multipart-attachments";
099:            public final static String RESPONSE_DELAY_PROPERTY = WsdlMockResponse.class
100:                    .getName()
101:                    + "@response-delay";
102:            public static final String STRIP_WHITESPACES = WsdlMockResponse.class
103:                    .getName()
104:                    + "@strip-whitespaces";
105:            public static final String REMOVE_EMPTY_CONTENT = WsdlMockResponse.class
106:                    .getName()
107:                    + "@remove_empty_content";
108:            public static final String ENCODE_ATTACHMENTS = WsdlMockResponse.class
109:                    .getName()
110:                    + "@encode_attachments";
111:
112:            protected List<FileAttachment> attachments = new ArrayList<FileAttachment>();
113:            private List<WsdlAttachmentPart> definedAttachmentParts;
114:            private ModelItemIconAnimator iconAnimator;
115:            private WsdlMockResult mockResult;
116:            private String responseContent;
117:            private ScriptEnginePool scriptEnginePool;
118:
119:            public WsdlMockResponse(WsdlMockOperation operation,
120:                    MockResponseConfig config) {
121:                super (config, operation, "/mockResponse.gif");
122:
123:                for (AttachmentConfig ac : getConfig().getAttachmentList()) {
124:                    attachments.add(new MockFileAttachment(ac, this ));
125:                }
126:
127:                if (!config.isSetEncoding())
128:                    config.setEncoding("UTF-8");
129:
130:                iconAnimator = new ModelItemIconAnimator(this ,
131:                        "/mockResponse.gif", new String[] {
132:                                "/exec_request_1.gif", "/exec_request_2.gif",
133:                                "/exec_request_3.gif", "/exec_request_4.gif" });
134:
135:                scriptEnginePool = new ScriptEnginePool(this );
136:                scriptEnginePool.setScript(getScript());
137:            }
138:
139:            public Attachment[] getAttachments() {
140:                return attachments.toArray(new Attachment[attachments.size()]);
141:            }
142:
143:            public String getScript() {
144:                return getConfig().isSetScript() ? getConfig().getScript()
145:                        .getStringValue() : null;
146:            }
147:
148:            public String getEncoding() {
149:                return getConfig().getEncoding();
150:            }
151:
152:            public void setEncoding(String encoding) {
153:                String old = getEncoding();
154:                getConfig().setEncoding(encoding);
155:                notifyPropertyChanged(ENCODING_PROPERTY, old, encoding);
156:            }
157:
158:            public String getResponseContent() {
159:                if (getConfig().getResponseContent() == null)
160:                    getConfig().addNewResponseContent();
161:
162:                if (responseContent == null)
163:                    responseContent = CompressedStringSupport
164:                            .getString(getConfig().getResponseContent());
165:
166:                return responseContent;
167:            }
168:
169:            public void setResponseContent(String responseContent) {
170:                String oldContent = getResponseContent();
171:                if (!responseContent.equals(oldContent)) {
172:                    this .responseContent = responseContent;
173:                    notifyPropertyChanged(RESPONSECONTENT_PROPERTY, oldContent,
174:                            responseContent);
175:                }
176:            }
177:
178:            @Override
179:            public ImageIcon getIcon() {
180:                return iconAnimator.getIcon();
181:            }
182:
183:            public WsdlMockOperation getMockOperation() {
184:                return (WsdlMockOperation) getParent();
185:            }
186:
187:            public WsdlMockResult execute(WsdlMockRequest request,
188:                    WsdlMockResult result) throws DispatchException {
189:                try {
190:                    iconAnimator.start();
191:
192:                    long delay = getResponseDelay();
193:                    if (delay > 0)
194:                        Thread.sleep(delay);
195:
196:                    String responseContent = getResponseContent();
197:                    String script = getScript();
198:                    if (script != null && script.trim().length() > 0) {
199:                        evaluateScript(request);
200:                    }
201:
202:                    // create merged context
203:                    WsdlSubmitContext context = new WsdlSubmitContext();
204:                    context.putAll(request.getContext());
205:                    context.putAll(request.getRequestContext());
206:
207:                    StringToStringMap responseHeaders = getResponseHeaders();
208:                    for (String name : responseHeaders.keySet()) {
209:                        result.addHeader(name, PropertyExpansionRequestFilter
210:                                .expandProperties(context, responseHeaders
211:                                        .get(name)));
212:                    }
213:
214:                    responseContent = PropertyExpansionRequestFilter
215:                            .expandProperties(context, responseContent);
216:
217:                    if (!result.isCommitted()) {
218:                        responseContent = writeResponse(result, responseContent);
219:                    }
220:
221:                    result.setResponseContent(responseContent);
222:
223:                    setMockResult(result);
224:
225:                    return mockResult;
226:                } catch (Throwable e) {
227:                    throw new DispatchException(e);
228:                } finally {
229:                    iconAnimator.stop();
230:                }
231:            }
232:
233:            public void evaluateScript(WsdlMockRequest request)
234:                    throws Exception {
235:                String script = getScript();
236:                if (script == null || script.trim().length() == 0)
237:                    return;
238:
239:                // unsatisfactory way of handling multiple threads -> create pool of
240:                // scripts instead
241:                WsdlMockService mockService = getMockOperation()
242:                        .getMockService();
243:                WsdlMockRunner mockRunner = mockService.getMockRunner();
244:                MockRunContext context = mockRunner == null ? new WsdlMockRunContext(
245:                        mockService, null)
246:                        : mockRunner.getMockContext();
247:
248:                SoapUIScriptEngine scriptEngine = scriptEnginePool
249:                        .getScriptEngine();
250:
251:                try {
252:                    scriptEngine.setVariable("context", request.getContext());
253:                    scriptEngine.setVariable("requestContext", request
254:                            .getRequestContext());
255:                    scriptEngine.setVariable("mockContext", context);
256:                    scriptEngine.setVariable("mockRequest", request);
257:                    scriptEngine.setVariable("mockResponse", this );
258:                    scriptEngine.setVariable("log", SoapUI.ensureGroovyLog());
259:
260:                    scriptEngine.run();
261:                } catch (RuntimeException e) {
262:                    throw new Exception(e.getMessage(), e);
263:                } finally {
264:                    scriptEnginePool.returnScriptEngine(scriptEngine);
265:                }
266:            }
267:
268:            @Override
269:            public void release() {
270:                super .release();
271:
272:                scriptEnginePool.release();
273:            }
274:
275:            public void setScript(String script) {
276:                String oldScript = getScript();
277:                if (!script.equals(oldScript)) {
278:                    if (!getConfig().isSetScript())
279:                        getConfig().addNewScript();
280:                    getConfig().getScript().setStringValue(script);
281:
282:                    scriptEnginePool.setScript(script);
283:
284:                    notifyPropertyChanged(SCRIPT_PROPERTY, oldScript, script);
285:                }
286:            }
287:
288:            public void setResponseHeaders(StringToStringMap headers) {
289:                StringToStringMap oldHeaders = getResponseHeaders();
290:
291:                HeaderConfig[] headerConfigs = new HeaderConfig[headers.size()];
292:                int ix = 0;
293:                for (String header : headers.keySet()) {
294:                    headerConfigs[ix] = HeaderConfig.Factory.newInstance();
295:                    headerConfigs[ix].setName(header);
296:                    headerConfigs[ix].setValue(headers.get(header));
297:                    ix++;
298:                }
299:
300:                getConfig().setHeaderArray(headerConfigs);
301:
302:                notifyPropertyChanged(HEADERS_PROPERTY, oldHeaders, headers);
303:            }
304:
305:            public StringToStringMap getResponseHeaders() {
306:                StringToStringMap result = new StringToStringMap();
307:                List<HeaderConfig> headerList = getConfig().getHeaderList();
308:                for (HeaderConfig header : headerList) {
309:                    result.put(header.getName(), header.getValue());
310:                }
311:
312:                return result;
313:            }
314:
315:            public MessagePart[] getResponseParts() {
316:                try {
317:                    // init
318:                    List<MessagePart> result = new ArrayList<MessagePart>();
319:                    WsdlOperation op = getMockOperation().getOperation();
320:                    WsdlContext wsdlContext = op.getInterface()
321:                            .getWsdlContext();
322:                    BindingOperation bindingOperation = op
323:                            .findBindingOperation(wsdlContext.getDefinition());
324:
325:                    if (bindingOperation == null)
326:                        return new MessagePart[0];
327:
328:                    // header parts
329:                    List<SoapHeader> headers = WsdlUtils
330:                            .getSoapHeaders(bindingOperation.getBindingOutput()
331:                                    .getExtensibilityElements());
332:
333:                    for (int i = 0; i < headers.size(); i++) {
334:                        SoapHeader header = headers.get(i);
335:
336:                        Message message = wsdlContext.getDefinition()
337:                                .getMessage(header.getMessage());
338:                        if (message == null) {
339:                            log.error("Missing message for header: "
340:                                    + header.getMessage());
341:                            continue;
342:                        }
343:
344:                        javax.wsdl.Part part = message
345:                                .getPart(header.getPart());
346:
347:                        if (part != null) {
348:                            SchemaType schemaType = WsdlUtils
349:                                    .getSchemaTypeForPart(wsdlContext, part);
350:                            if (schemaType != null)
351:                                result.add(new WsdlHeaderPart(part.getName(),
352:                                        schemaType, part.getElementName()));
353:                        } else
354:                            log.error("Missing part for header; "
355:                                    + header.getPart());
356:                    }
357:
358:                    // content parts
359:                    javax.wsdl.Part[] parts = WsdlUtils
360:                            .getOutputParts(bindingOperation);
361:
362:                    for (int i = 0; i < parts.length; i++) {
363:                        javax.wsdl.Part part = parts[i];
364:
365:                        if (!WsdlUtils.isAttachmentOutputPart(part,
366:                                bindingOperation)) {
367:                            SchemaType schemaType = WsdlUtils
368:                                    .getSchemaTypeForPart(wsdlContext, part);
369:                            if (schemaType != null)
370:                                result.add(new WsdlContentPart(part.getName(),
371:                                        schemaType, part.getElementName()));
372:                        }
373:                    }
374:
375:                    result.addAll(Arrays.asList(getDefinedAttachmentParts()));
376:
377:                    return result.toArray(new MessagePart[result.size()]);
378:                } catch (Exception e) {
379:                    SoapUI.logError(e);
380:                    return new MessagePart[0];
381:                }
382:            }
383:
384:            public Attachment attachFile(File file, boolean cache) {
385:                try {
386:                    FileAttachment fileAttachment = new MockFileAttachment(
387:                            file, cache, this );
388:                    attachments.add(fileAttachment);
389:                    notifyPropertyChanged(ATTACHMENTS_PROPERTY, null,
390:                            fileAttachment);
391:                    return fileAttachment;
392:                } catch (IOException e) {
393:                    UISupport.showErrorMessage(e);
394:                    return null;
395:                }
396:            }
397:
398:            public int getAttachmentCount() {
399:                return attachments.size();
400:            }
401:
402:            public WsdlAttachment getAttachmentAt(int index) {
403:                return attachments.get(index);
404:            }
405:
406:            public void removeAttachment(Attachment attachment) {
407:                int ix = attachments.indexOf(attachment);
408:                attachments.remove(ix);
409:
410:                try {
411:                    notifyPropertyChanged(ATTACHMENTS_PROPERTY, attachment,
412:                            null);
413:                } finally {
414:                    getConfig().removeAttachment(ix);
415:                }
416:            }
417:
418:            public WsdlAttachmentPart[] getDefinedAttachmentParts() {
419:                if (definedAttachmentParts == null) {
420:                    try {
421:                        WsdlOperation operation = getMockOperation()
422:                                .getOperation();
423:                        if (operation == null) {
424:                            definedAttachmentParts = new ArrayList<WsdlAttachmentPart>();
425:                        } else {
426:                            UISupport.setHourglassCursor();
427:                            definedAttachmentParts = AttachmentUtils
428:                                    .extractAttachmentParts(operation,
429:                                            getResponseContent(), true, true);
430:                        }
431:                    } catch (Exception e) {
432:                        log.warn(e.toString());
433:                    } finally {
434:                        UISupport.resetCursor();
435:                    }
436:                }
437:
438:                return definedAttachmentParts
439:                        .toArray(new WsdlAttachmentPart[definedAttachmentParts
440:                                .size()]);
441:            }
442:
443:            public WsdlAttachmentPart getAttachmentPart(String partName) {
444:                WsdlAttachmentPart[] parts = getDefinedAttachmentParts();
445:                for (WsdlAttachmentPart part : parts) {
446:                    if (part.getName().equals(partName))
447:                        return part;
448:                }
449:
450:                return null;
451:            }
452:
453:            public Attachment[] getAttachmentsForPart(String partName) {
454:                List<Attachment> result = new ArrayList<Attachment>();
455:
456:                for (Attachment attachment : attachments) {
457:                    if (attachment.getPart().equals(partName))
458:                        result.add(attachment);
459:                }
460:
461:                return result.toArray(new Attachment[result.size()]);
462:            }
463:
464:            public boolean isMtomEnabled() {
465:                return getSettings().getBoolean(WsdlSettings.ENABLE_MTOM);
466:            }
467:
468:            public void setMtomEnabled(boolean mtomEnabled) {
469:                boolean old = isMtomEnabled();
470:                getSettings().setBoolean(WsdlSettings.ENABLE_MTOM, mtomEnabled);
471:                definedAttachmentParts = null;
472:                notifyPropertyChanged(MTOM_NABLED_PROPERTY, old, mtomEnabled);
473:            }
474:
475:            private String writeResponse(WsdlMockResult response,
476:                    String responseContent) throws Exception {
477:                MimeMultipart mp = null;
478:                WsdlOperation operation = getMockOperation().getOperation();
479:                if (operation == null)
480:                    throw new Exception(
481:                            "Missing WsdlOperation for mock response");
482:
483:                SoapVersion soapVersion = operation.getInterface()
484:                        .getSoapVersion();
485:
486:                StringToStringMap contentIds = new StringToStringMap();
487:                boolean isXOP = false;
488:
489:                // preprocess only if neccessary
490:                if (isMtomEnabled() || getAttachmentCount() > 0) {
491:                    try {
492:                        mp = new MimeMultipart();
493:
494:                        MessageXmlObject requestXmlObject = new MessageXmlObject(
495:                                (WsdlOperation) operation,
496:                                getResponseContent(), true);
497:                        MessageXmlPart[] requestParts = requestXmlObject
498:                                .getMessageParts();
499:                        for (MessageXmlPart requestPart : requestParts) {
500:                            if (AttachmentUtils.prepareMessagePart(this , mp,
501:                                    requestPart, contentIds))
502:                                isXOP = true;
503:                        }
504:                        responseContent = requestXmlObject.getMessageContent();
505:                    } catch (Exception e) {
506:                        log.warn("Failed to process inline/MTOM attachments; "
507:                                + e);
508:                    }
509:                }
510:
511:                response.initResponse();
512:
513:                if (isRemoveEmptyContent()) {
514:                    responseContent = RemoveEmptyContentRequestFilter
515:                            .removeEmptyContent(responseContent);
516:                }
517:
518:                if (isStripWhitespaces()) {
519:                    responseContent = XmlUtils
520:                            .stripWhitespaces(responseContent);
521:                }
522:
523:                // non-multipart request?
524:                if (!isXOP && (mp == null || mp.getCount() == 0)
525:                        && getAttachmentCount() == 0) {
526:                    String encoding = getEncoding();
527:                    byte[] content = encoding == null ? responseContent
528:                            .getBytes() : responseContent.getBytes(encoding);
529:
530:                    response.setContentType(soapVersion
531:                            .getContentTypeHttpHeader(encoding));
532:
533:                    String acceptEncoding = response.getMockRequest()
534:                            .getRequestHeaders().get("Accept-Encoding");
535:                    if (acceptEncoding != null
536:                            && acceptEncoding.toUpperCase().contains("GZIP")) {
537:                        response.addHeader("Content-Encoding", "gzip");
538:                        GZIPOutputStream zipOut = new GZIPOutputStream(response
539:                                .getOutputStream());
540:                        zipOut.write(content);
541:                        zipOut.close();
542:                    } else {
543:                        response.getOutputStream().write(content);
544:                    }
545:                } else {
546:                    // make sure..
547:                    if (mp == null)
548:                        mp = new MimeMultipart();
549:
550:                    // init root part
551:                    initRootPart(responseContent, mp, isXOP);
552:
553:                    // init mimeparts
554:                    AttachmentUtils.addMimeParts(this , mp, contentIds);
555:
556:                    // create request message
557:                    MimeMessage message = new MimeMessage(
558:                            AttachmentUtils.JAVAMAIL_SESSION);
559:                    message.setContent(mp);
560:                    message.saveChanges();
561:                    MimeMessageMockResponseEntity mimeMessageRequestEntity = new MimeMessageMockResponseEntity(
562:                            message, isXOP, this );
563:
564:                    response.addHeader("Content-Type", mimeMessageRequestEntity
565:                            .getContentType());
566:                    response.addHeader("MIME-Version", "1.0");
567:                    mimeMessageRequestEntity.writeRequest(response
568:                            .getOutputStream());
569:                }
570:
571:                return responseContent;
572:            }
573:
574:            private void initRootPart(String requestContent, MimeMultipart mp,
575:                    boolean isXOP) throws MessagingException {
576:                MimeBodyPart rootPart = new PreencodedMimeBodyPart("8bit");
577:                rootPart.setContentID(AttachmentUtils.ROOTPART_SOAPUI_ORG);
578:                mp.addBodyPart(rootPart, 0);
579:
580:                DataHandler dataHandler = new DataHandler(
581:                        new MockResponseDataSource(this , requestContent, isXOP));
582:                rootPart.setDataHandler(dataHandler);
583:            }
584:
585:            public Attachment addAttachment(Attachment attachment) {
586:                if (attachment instanceof  BodyPartAttachment) {
587:                    try {
588:                        BodyPartAttachment att = (BodyPartAttachment) attachment;
589:
590:                        AttachmentConfig newConfig = (AttachmentConfig) getConfig()
591:                                .addNewAttachment();
592:                        newConfig
593:                                .setData(Tools.readAll(att.getInputStream(), 0)
594:                                        .toByteArray());
595:                        newConfig.setContentId(att.getContentID());
596:                        newConfig.setContentType(att.getContentType());
597:                        newConfig.setName(att.getName());
598:
599:                        FileAttachment newAttachment = new MockFileAttachment(
600:                                newConfig, this );
601:                        attachments.add(newAttachment);
602:                        return newAttachment;
603:                    } catch (Exception e) {
604:                        SoapUI.logError(e);
605:                    }
606:                } else if (attachment instanceof  FileAttachment) {
607:                    AttachmentConfig oldConfig = ((FileAttachment) attachment)
608:                            .getConfig();
609:                    AttachmentConfig newConfig = (AttachmentConfig) getConfig()
610:                            .addNewAttachment().set(oldConfig);
611:                    FileAttachment newAttachment = new MockFileAttachment(
612:                            newConfig, this );
613:                    attachments.add(newAttachment);
614:                    return newAttachment;
615:                }
616:
617:                return null;
618:            }
619:
620:            public void setResponseDelay(long delay) {
621:                long oldDelay = getResponseDelay();
622:
623:                if (delay == 0)
624:                    getSettings().clearSetting(RESPONSE_DELAY_PROPERTY);
625:                else
626:                    getSettings().setLong(RESPONSE_DELAY_PROPERTY, delay);
627:
628:                notifyPropertyChanged(RESPONSE_DELAY_PROPERTY, oldDelay, delay);
629:            }
630:
631:            public long getResponseDelay() {
632:                return getSettings().getLong(RESPONSE_DELAY_PROPERTY, 0);
633:            }
634:
635:            public void setMockResult(WsdlMockResult mockResult) {
636:                WsdlMockResult oldResult = this .mockResult;
637:                this .mockResult = mockResult;
638:                notifyPropertyChanged(MOCKRESULT_PROPERTY, oldResult,
639:                        mockResult);
640:            }
641:
642:            public WsdlMockResult getMockResult() {
643:                return mockResult;
644:            }
645:
646:            public long getContentLength() {
647:                return getResponseContent().length();
648:            }
649:
650:            public boolean isMultipartEnabled() {
651:                return !getSettings().getBoolean(DISABLE_MULTIPART_ATTACHMENTS);
652:            }
653:
654:            public void setMultipartEnabled(boolean multipartEnabled) {
655:                getSettings().setBoolean(DISABLE_MULTIPART_ATTACHMENTS,
656:                        multipartEnabled);
657:            }
658:
659:            public boolean isRemoveEmptyContent() {
660:                return getSettings().getBoolean(REMOVE_EMPTY_CONTENT);
661:            }
662:
663:            public void setRemoveEmptyContent(boolean removeEmptyContent) {
664:                boolean old = getSettings().getBoolean(REMOVE_EMPTY_CONTENT);
665:                getSettings().setBoolean(REMOVE_EMPTY_CONTENT,
666:                        removeEmptyContent);
667:                notifyPropertyChanged(REMOVE_EMPTY_CONTENT, old,
668:                        removeEmptyContent);
669:            }
670:
671:            public boolean isEncodeAttachments() {
672:                return getSettings().getBoolean(ENCODE_ATTACHMENTS);
673:            }
674:
675:            public void setEncodeAttachments(boolean encodeAttachments) {
676:                boolean old = getSettings().getBoolean(ENCODE_ATTACHMENTS);
677:                getSettings().setBoolean(ENCODE_ATTACHMENTS, encodeAttachments);
678:                notifyPropertyChanged(ENCODE_ATTACHMENTS, old,
679:                        encodeAttachments);
680:            }
681:
682:            public boolean isStripWhitespaces() {
683:                return getSettings().getBoolean(STRIP_WHITESPACES);
684:            }
685:
686:            public void setStripWhitespaces(boolean stripWhitespaces) {
687:                boolean old = getSettings().getBoolean(STRIP_WHITESPACES);
688:                getSettings().setBoolean(STRIP_WHITESPACES, stripWhitespaces);
689:                notifyPropertyChanged(STRIP_WHITESPACES, old, stripWhitespaces);
690:            }
691:
692:            @Override
693:            public void onSave() {
694:                if (responseContent != null) {
695:                    CompressedStringSupport.setString(getConfig()
696:                            .getResponseContent(), responseContent);
697:                    responseContent = null;
698:                }
699:            }
700:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.