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 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 getRequestContent()
45  	{
46  		if( requestContent != null )
47  			return requestContent;
48  
49  		if( response == null )
50  			response = getModelItem().getResponse();
51  
52  		return response == null ? getModelItem().getRequestContent() : response.getRequestContent();
53  	}
54  
55  	@Override
56  	public String getResponseContentAsXml()
57  	{
58  		if( response == null )
59  			response = getModelItem().getResponse();
60  
61  		return response.getContentAsXml();
62  	}
63  
64  	public StringToStringMap getRequestHeaders()
65  	{
66  		return response == null ? getModelItem().getRequestHeaders() : response.getRequestHeaders();
67  	}
68  
69  	public Attachment[] getRequestAttachments()
70  	{
71  		return getModelItem().getAttachments();
72  	}
73  
74  	public Attachment[] getResponseAttachments()
75  	{
76  		if( response == null )
77  			response = getModelItem().getResponse();
78  
79  		return response == null ? null : response.getAttachments();
80  	}
81  
82  	public String getResponseContent()
83  	{
84  		if( response == null )
85  			response = getModelItem().getResponse();
86  
87  		return response == null ? null : response.getContentAsString();
88  	}
89  
90  	public HttpResponse getResponse()
91  	{
92  		return response;
93  	}
94  
95  	public void setResponse( HttpResponse response )
96  	{
97  		this.response = response;
98  	}
99  
100 	public StringToStringMap getResponseHeaders()
101 	{
102 		if( response == null )
103 			response = getModelItem().getResponse();
104 
105 		return response == null ? null : response.getResponseHeaders();
106 	}
107 
108 	public long getTimeTaken()
109 	{
110 		if( response == null )
111 			response = getModelItem().getResponse();
112 
113 		return response == null ? 0 : response.getTimeTaken();
114 	}
115 
116 	public long getTimestamp()
117 	{
118 		if( response == null )
119 			response = getModelItem().getResponse();
120 
121 		return response == null ? 0 : response.getTimestamp();
122 	}
123 
124 	public boolean isDiscarded()
125 	{
126 		return false;
127 	}
128 
129 	public Operation getOperation()
130 	{
131 		return null;
132 	}
133 
134 	public int getResponseStatusCode()
135 	{
136 		return response == null ? 0 : response.getStatusCode();
137 	}
138 
139 	public String getResponseContentType()
140 	{
141 		return response == null ? null : response.getContentType();
142 	}
143 
144 	public boolean hasRawData()
145 	{
146 		return false;
147 	}
148 
149 	public byte[] getRawRequestData()
150 	{
151 		return null;
152 	}
153 
154 	public byte[] getRawResponseData()
155 	{
156 		return null;
157 	}
158 
159 	public Attachment[] getResponseAttachmentsForPart( String name )
160 	{
161 		List<Attachment> result = new ArrayList<Attachment>();
162 
163 		for( Attachment attachment : getResponseAttachments() )
164 		{
165 			if( attachment.getPart().equals( name ) )
166 				result.add( attachment );
167 		}
168 
169 		return result.toArray( new Attachment[result.size()] );
170 	}
171 
172 	public Attachment[] getRequestAttachmentsForPart( String name )
173 	{
174 		List<Attachment> result = new ArrayList<Attachment>();
175 
176 		for( Attachment attachment : getRequestAttachments() )
177 		{
178 			if( attachment.getPart().equals( name ) )
179 				result.add( attachment );
180 		}
181 
182 		return result.toArray( new Attachment[result.size()] );
183 	}
184 
185 	public boolean hasRequest( boolean ignoreEmpty )
186 	{
187 		String requestContent = getRequestContent();
188 		return !( requestContent == null || ( ignoreEmpty && requestContent.trim().length() == 0 ) );
189 	}
190 
191 	public boolean hasResponse()
192 	{
193 		String responseContent = getResponseContent();
194 		return responseContent != null && responseContent.trim().length() > 0;
195 	}
196 }