View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  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.teststeps;
14  
15  import com.eviware.soapui.impl.rest.RestRequestInterface;
16  import com.eviware.soapui.impl.rest.RestResource;
17  import com.eviware.soapui.impl.wsdl.submit.AbstractRestMessageExchange;
18  import com.eviware.soapui.impl.wsdl.submit.transports.http.HttpResponse;
19  import com.eviware.soapui.model.iface.Attachment;
20  import com.eviware.soapui.model.iface.Operation;
21  import com.eviware.soapui.support.types.StringToStringMap;
22  import com.eviware.soapui.support.xml.XmlUtils;
23  
24  public class RestResponseMessageExchange extends AbstractRestMessageExchange<RestRequestInterface>
25  {
26  	private HttpResponse response;
27  	private String requestContent;
28  
29  	public RestResponseMessageExchange( RestRequestInterface request )
30  	{
31  		super( request );
32  
33  		response = request.getResponse();
34  		if( response != null )
35  		{
36  			for( String key : response.getPropertyNames() )
37  			{
38  				addProperty( key, response.getProperty( key ) );
39  			}
40  		}
41  	}
42  
43  	public String getRequestContent()
44  	{
45  		if( requestContent != null )
46  			return requestContent;
47  
48  		if( response == null )
49  			response = getModelItem().getResponse();
50  
51  		return response == null ? getModelItem().getRequestContent() : response.getRequestContent();
52  	}
53  
54  	public StringToStringMap getRequestHeaders()
55  	{
56  		return response == null ? getModelItem().getRequestHeaders() : response.getRequestHeaders();
57  	}
58  
59  	public Attachment[] getRequestAttachments()
60  	{
61  		return getModelItem().getAttachments();
62  	}
63  
64  	public Attachment[] getResponseAttachments()
65  	{
66  		if( response == null )
67  			response = getModelItem().getResponse();
68  
69  		return response == null ? null : response.getAttachments();
70  	}
71  
72  	public String getResponseContent()
73  	{
74  		if( response == null )
75  			response = getModelItem().getResponse();
76  
77  		return response == null ? null : response.getContentAsString();
78  	}
79  
80  	public HttpResponse getResponse()
81  	{
82  		return response;
83  	}
84  
85  	public String getRequestContentAsXml()
86  	{
87  		String result = getRequestContent();
88  		return XmlUtils.seemsToBeXml( result ) ? result : "<not-xml/>";
89  	}
90  
91  	public void setResponse( HttpResponse response )
92  	{
93  		this.response = response;
94  	}
95  
96  	public String getResponseContentAsXml()
97  	{
98  		if( response == null )
99  			response = getModelItem().getResponse();
100 
101 		return response == null ? null : response.getContentAsXml();
102 	}
103 
104 	public StringToStringMap getResponseHeaders()
105 	{
106 		if( response == null )
107 			response = getModelItem().getResponse();
108 
109 		return response == null ? null : response.getResponseHeaders();
110 	}
111 
112 	public long getTimeTaken()
113 	{
114 		if( response == null )
115 			response = getModelItem().getResponse();
116 
117 		return response == null ? 0 : response.getTimeTaken();
118 	}
119 
120 	public long getTimestamp()
121 	{
122 		if( response == null )
123 			response = getModelItem().getResponse();
124 
125 		return response == null ? 0 : response.getTimestamp();
126 	}
127 
128 	public void setRequestContent( String requestContent )
129 	{
130 		this.requestContent = requestContent;
131 	}
132 
133 	public boolean isDiscarded()
134 	{
135 		return false;
136 	}
137 
138 	public RestResource getResource()
139 	{
140 		return getModelItem().getResource();
141 	}
142 
143 	public RestRequestInterface getRestRequest()
144 	{
145 		return getModelItem();
146 	}
147 
148 	public Operation getOperation()
149 	{
150 		return getResource();
151 	}
152 
153 	public int getResponseStatusCode()
154 	{
155 		return response == null ? 0 : response.getStatusCode();
156 	}
157 
158 	public String getResponseContentType()
159 	{
160 		return response == null ? null : response.getContentType();
161 	}
162 }