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  package com.eviware.soapui.impl.wsdl.submit.transports.http;
13  
14  import java.io.File;
15  import java.io.FileOutputStream;
16  import java.io.IOException;
17  import java.io.OutputStream;
18  import java.net.MalformedURLException;
19  import java.net.URL;
20  import java.util.ArrayList;
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  import com.eviware.soapui.SoapUI;
26  import com.eviware.soapui.impl.support.AbstractHttpRequest;
27  import com.eviware.soapui.impl.support.http.HttpRequest;
28  import com.eviware.soapui.impl.wsdl.support.RequestFileAttachment;
29  import com.eviware.soapui.model.iface.Attachment;
30  import com.eviware.soapui.model.iface.Request;
31  import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
32  import com.gargoylesoftware.htmlunit.WebClient;
33  import com.gargoylesoftware.htmlunit.WebRequestSettings;
34  import com.gargoylesoftware.htmlunit.html.HtmlElement;
35  import com.gargoylesoftware.htmlunit.html.HtmlPage;
36  
37  public class HTMLPageSourceDownloader
38  {
39  	WebClient client = new WebClient();
40  	List<String> missingResourcesList = new ArrayList<String>();
41  	public static final String MISSING_RESOURCES_LIST = "MissingResourcesList";
42  
43  	public static final HashMap<String, String> acceptTypes = new HashMap<String, String>()
44  	{
45  		{
46  			put( "html", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" );
47  			put( "img", "image/png,image/*;q=0.8,*/*;q=0.5" );
48  			put( "script", "*/*" );
49  			put( "style", "text/css,*/*;q=0.1" );
50  		}
51  	};
52  
53  	List<Attachment> attachmentList = new ArrayList<Attachment>();
54  
55  	protected List<Attachment> downloadCssAndImages( String endpoint, HttpRequest request )
56  			throws  MalformedURLException, IOException
57  	{
58  		HtmlPage htmlPage = client.getPage( endpoint );
59  		String xPathExpression = "//*[name() = 'img' or name() = 'link' and @type = 'text/css']";
60  		List<?> resultList = htmlPage.getByXPath( xPathExpression );
61  		byte[] bytes = null;
62  		List<Attachment> attachmentList = new ArrayList<Attachment>();
63  		Iterator<?> i = resultList.iterator();
64  		while( i.hasNext() )
65  		{
66  			try
67  			{
68  				HtmlElement htmlElement = ( HtmlElement )i.next();
69  				String path = htmlElement.getAttribute( "src" ).equals( "" ) ? htmlElement.getAttribute( "href" )
70  						: htmlElement.getAttribute( "src" );
71  				if( path == null || path.equals( "" ) )
72  					continue;
73  				URL url = htmlPage.getFullyQualifiedUrl( path );
74  				try
75  				{
76  					bytes = downloadResource( htmlPage, htmlElement, url );
77  				}
78  				catch( FailingHttpStatusCodeException fhsce )
79  				{
80  					SoapUI.log.warn( fhsce.getMessage() );
81  					attachmentList.add( createMissingAttachment( request, url, fhsce ) );
82  					continue;
83  				}
84  
85  				attachmentList.add( createAttachment( bytes, url, request ) );
86  			}
87  			catch( Exception e )
88  			{
89  				SoapUI.logError( e );
90  			}
91  		}
92  		client.removeRequestHeader( "Accept" );
93  		return attachmentList;
94  	}
95  
96  	private RequestFileAttachment createMissingAttachment( HttpRequest request, URL url,
97  			FailingHttpStatusCodeException fhsce ) throws IOException
98  	{
99  		File temp = new File( fhsce.getStatusCode() + "_" + fhsce.getStatusMessage() + "_" + url.toString() );
100 		RequestFileAttachment missingFile = new RequestFileAttachment( temp, false, ( AbstractHttpRequest<?> )request );
101 		missingResourcesList.add(  fhsce.getStatusCode() + " " + fhsce.getStatusMessage() + " " + url.toString());
102 		return missingFile;
103 	}
104 
105 	public Attachment createAttachment( byte[] bytes, URL url, Request request ) throws IOException
106 	{
107 		String fileName = url.getPath()
108 				.substring( url.getPath().lastIndexOf( "/" ) + 1, url.getPath().lastIndexOf( "." ) );
109 		String extension = url.getPath().substring( url.getPath().lastIndexOf( "." ) );
110 
111 		// handling -> java.lang.IllegalArgumentException: Prefix string too short
112 		if( fileName.length() < 3 )
113 		{
114 			fileName += "___";
115 		}
116 
117 		File temp = File.createTempFile( fileName, extension );
118 		OutputStream out = new FileOutputStream( temp );
119 		out.write( bytes );
120 		out.close();
121 		return new RequestFileAttachment( temp, false, ( AbstractHttpRequest<?> )request );
122 	}
123 
124 	private byte[] downloadResource( HtmlPage page, HtmlElement htmlElement, URL url ) throws IOException
125 	{
126 		WebRequestSettings wrs = null;
127 
128 		wrs = new WebRequestSettings( url );
129 		wrs.setAdditionalHeader( "Referer", page.getWebResponse().getRequestSettings().getUrl().toString() );
130 		client.addRequestHeader( "Accept", acceptTypes.get( htmlElement.getTagName().toLowerCase() ) );
131 		return client.getPage( wrs ).getWebResponse().getContentAsBytes();
132 
133 	}
134 
135 	public List<String> getMissingResourcesList()
136 	{
137 		return missingResourcesList;
138 	}
139 
140 }