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 java.util.ArrayList;
16  import java.util.List;
17  
18  import com.eviware.soapui.impl.support.http.HttpRequestInterface;
19  import com.eviware.soapui.impl.wsdl.submit.AbstractMessageExchange;
20  import com.eviware.soapui.impl.wsdl.submit.transports.http.HttpResponse;
21  import com.eviware.soapui.model.iface.Attachment;
22  import com.eviware.soapui.model.iface.Operation;
23  import com.eviware.soapui.support.types.StringToStringMap;
24  
25  public class HttpResponseMessageExchange extends AbstractMessageExchange<HttpRequestInterface<?>>
26  {
27  	private HttpResponse response;
28  	private String requestContent;
29  
30  	public HttpResponseMessageExchange( HttpRequestInterface<?> request )
31  	{
32  		super( request );
33  
34  		response = request.getResponse();
35  		if( response != null )
36  		{
37  			for( String key : response.getPropertyNames() )
38  			{
39  				addProperty( key, response.getProperty( key ) );
40  			}
41  		}
42  	}
43  
44  	public String getEndpoint()
45  	{
46  		return response == null ? null : response.getURL().toString();
47  	}
48  
49  	public String getRequestContent()
50  	{
51  		if( requestContent != null )
52  			return requestContent;
53  
54  		if( response == null )
55  			response = getModelItem().getResponse();
56  
57  		return response == null ? getModelItem().getRequestContent() : response.getRequestContent();
58  	}
59  
60  	@Override
61  	public String getResponseContentAsXml()
62  	{
63  		if( response == null )
64  			response = getModelItem().getResponse();
65  
66  		return response.getContentAsXml();
67  	}
68  
69  	public StringToStringMap getRequestHeaders()
70  	{
71  		return response == null ? getModelItem().getRequestHeaders() : response.getRequestHeaders();
72  	}
73  
74  	public Attachment[] getRequestAttachments()
75  	{
76  		return getModelItem().getAttachments();
77  	}
78  
79  	public Attachment[] getResponseAttachments()
80  	{
81  		if( response == null )
82  			response = getModelItem().getResponse();
83  
84  		return response == null ? null : response.getAttachments();
85  	}
86  
87  	public String getResponseContent()
88  	{
89  		if( response == null )
90  			response = getModelItem().getResponse();
91  
92  		return response == null ? null : response.getContentAsString();
93  	}
94  
95  	public HttpResponse getResponse()
96  	{
97  		return response;
98  	}
99  
100 	public void setResponse( HttpResponse response )
101 	{
102 		this.response = response;
103 	}
104 
105 	public StringToStringMap getResponseHeaders()
106 	{
107 		if( response == null )
108 			response = getModelItem().getResponse();
109 
110 		return response == null ? null : response.getResponseHeaders();
111 	}
112 
113 	public long getTimeTaken()
114 	{
115 		if( response == null )
116 			response = getModelItem().getResponse();
117 
118 		return response == null ? 0 : response.getTimeTaken();
119 	}
120 
121 	public long getTimestamp()
122 	{
123 		if( response == null )
124 			response = getModelItem().getResponse();
125 
126 		return response == null ? 0 : response.getTimestamp();
127 	}
128 
129 	public boolean isDiscarded()
130 	{
131 		return false;
132 	}
133 
134 	public Operation getOperation()
135 	{
136 		return null;
137 	}
138 
139 	public int getResponseStatusCode()
140 	{
141 		return response == null ? 0 : response.getStatusCode();
142 	}
143 
144 	public String getResponseContentType()
145 	{
146 		return response == null ? null : response.getContentType();
147 	}
148 
149 	public boolean hasRawData()
150 	{
151 		return false;
152 	}
153 
154 	public byte[] getRawRequestData()
155 	{
156 		return null;
157 	}
158 
159 	public byte[] getRawResponseData()
160 	{
161 		return null;
162 	}
163 
164 	public Attachment[] getResponseAttachmentsForPart( String name )
165 	{
166 		List<Attachment> result = new ArrayList<Attachment>();
167 
168 		if( getResponseAttachments() != null )
169 		{
170 		for( Attachment attachment : getResponseAttachments() )
171 		{
172 			if( attachment.getPart().equals( name ) )
173 				result.add( attachment );
174 		}}
175 
176 		return result.toArray( new Attachment[result.size()] );
177 	}
178 
179 	public Attachment[] getRequestAttachmentsForPart( String name )
180 	{
181 		List<Attachment> result = new ArrayList<Attachment>();
182 
183 		for( Attachment attachment : getRequestAttachments() )
184 		{
185 			if( attachment.getPart().equals( name ) )
186 				result.add( attachment );
187 		}
188 
189 		return result.toArray( new Attachment[result.size()] );
190 	}
191 
192 	public boolean hasRequest( boolean ignoreEmpty )
193 	{
194 		String requestContent = getRequestContent();
195 		return !( requestContent == null || ( ignoreEmpty && requestContent.trim().length() == 0 ) );
196 	}
197 
198 	public boolean hasResponse()
199 	{
200 		String responseContent = getResponseContent();
201 		return responseContent != null && responseContent.trim().length() > 0;
202 	}
203 }