View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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 getEndpoint()
44  	{
45  		return response == null ? null : response.getURL().toString();
46  	}
47  	
48  	public String getRequestContent()
49  	{
50  		if( requestContent != null )
51  			return requestContent;
52  
53  		if( response == null )
54  			response = getModelItem().getResponse();
55  
56  		return response == null ? getModelItem().getRequestContent() : response.getRequestContent();
57  	}
58  
59  	public StringToStringMap getRequestHeaders()
60  	{
61  		return response == null ? getModelItem().getRequestHeaders() : response.getRequestHeaders();
62  	}
63  
64  	public Attachment[] getRequestAttachments()
65  	{
66  		return getModelItem().getAttachments();
67  	}
68  
69  	public Attachment[] getResponseAttachments()
70  	{
71  		if( response == null )
72  			response = getModelItem().getResponse();
73  
74  		return response == null ? null : response.getAttachments();
75  	}
76  
77  	public String getResponseContent()
78  	{
79  		if( response == null )
80  			response = getModelItem().getResponse();
81  
82  		return response == null ? null : response.getContentAsString();
83  	}
84  
85  	public HttpResponse getResponse()
86  	{
87  		return response;
88  	}
89  
90  	public String getRequestContentAsXml()
91  	{
92  		String result = getRequestContent();
93  		return XmlUtils.seemsToBeXml( result ) ? result : "<not-xml/>";
94  	}
95  
96  	public void setResponse( HttpResponse response )
97  	{
98  		this.response = response;
99  	}
100 
101 	public String getResponseContentAsXml()
102 	{
103 		if( response == null )
104 			response = getModelItem().getResponse();
105 
106 		return response == null ? null : response.getContentAsXml();
107 	}
108 
109 	public StringToStringMap getResponseHeaders()
110 	{
111 		if( response == null )
112 			response = getModelItem().getResponse();
113 
114 		return response == null ? null : response.getResponseHeaders();
115 	}
116 
117 	public long getTimeTaken()
118 	{
119 		if( response == null )
120 			response = getModelItem().getResponse();
121 
122 		return response == null ? 0 : response.getTimeTaken();
123 	}
124 
125 	public long getTimestamp()
126 	{
127 		if( response == null )
128 			response = getModelItem().getResponse();
129 
130 		return response == null ? 0 : response.getTimestamp();
131 	}
132 
133 	public void setRequestContent( String requestContent )
134 	{
135 		this.requestContent = requestContent;
136 	}
137 
138 	public boolean isDiscarded()
139 	{
140 		return false;
141 	}
142 
143 	public RestResource getResource()
144 	{
145 		return getModelItem().getResource();
146 	}
147 
148 	public RestRequestInterface getRestRequest()
149 	{
150 		return getModelItem();
151 	}
152 
153 	public Operation getOperation()
154 	{
155 		return getResource();
156 	}
157 
158 	public int getResponseStatusCode()
159 	{
160 		return response == null ? 0 : response.getStatusCode();
161 	}
162 
163 	public String getResponseContentType()
164 	{
165 		return response == null ? null : response.getContentType();
166 	}
167 }