X-Git-Url: http://jaekl.net/gitweb/?p=frank.git;a=blobdiff_plain;f=report%2Fnet%2Fjaekl%2Fqd%2Fhttp%2FRequestBroker.html;fp=report%2Fnet%2Fjaekl%2Fqd%2Fhttp%2FRequestBroker.html;h=0000000000000000000000000000000000000000;hp=7dcf324286bfc9ab7eb8569baec4ad7506044c82;hb=c35af9e511e8390a66ca76331c3af43d0f0f7464;hpb=24f097b0fe78fa44f99b9f6f2e51a8e689d86a4a diff --git a/report/net/jaekl/qd/http/RequestBroker.html b/report/net/jaekl/qd/http/RequestBroker.html deleted file mode 100644 index 7dcf324..0000000 --- a/report/net/jaekl/qd/http/RequestBroker.html +++ /dev/null @@ -1,890 +0,0 @@ - - -tests coverage - - - - - - - - - -
-Frames -No Frames -
-

- - - - - - - - - - - - - - - -
 %method%block%branch%line
net.jaekl.qd.http.RequestBroker
83%(5/6)
50%(14/28)
40%(4/10)
53%(31/58)
-

-

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
hit countmethod namemethod modifiersmethod signature
1<clinit>[static]void <clinit>()
9<init>[public]void <init>(java.lang.String,java.util.ArrayList)
0doSubmit[]java.io.InputStream doSubmit(java.lang.String,java.util.ArrayList)
1submit[public]java.lang.String submit(java.lang.String,java.util.ArrayList)
5submitAndParse[public]net.jaekl.qd.xml.ParseResult submitAndParse(java.lang.String,java.util.ArrayList,java.lang.Class)
4submitAndParse[public]net.jaekl.qd.xml.ParseResult submitAndParse(java.lang.String,java.util.ArrayList,java.lang.Class,java.lang.String)
-

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 1 
 // Copyright (C) 2004, 2014 Christian Jaekl
 2 
 
 3 
 package net.jaekl.qd.http;
 4 
 
 5 
 import java.io.BufferedReader;
 6 
 import java.io.IOException;
 7 
 import java.io.InputStream;
 8 
 import java.io.InputStreamReader;
 9 
 import java.io.UnsupportedEncodingException;
 10 
 import java.lang.reflect.InvocationTargetException;
 11 
 import java.util.ArrayList;
 12 
 
 13 
 import net.jaekl.qd.QDException;
 14 
 import net.jaekl.qd.util.ExceptionUtils;
 15 
 import net.jaekl.qd.xml.ParseHandler;
 16 
 import net.jaekl.qd.xml.ParseResult;
 17 
 
 18 
 import org.apache.http.HttpEntity;
 19 
 import org.apache.http.HttpResponse;
 20 
 import org.apache.http.NameValuePair;
 21 
 import org.apache.http.client.HttpClient;
 22 
 import org.apache.http.client.config.RequestConfig;
 23 
 import org.apache.http.client.entity.UrlEncodedFormEntity;
 24 
 import org.apache.http.client.methods.HttpPost;
 25 
 import org.apache.http.impl.client.HttpClientBuilder;
 26 
 import org.xml.sax.InputSource;
 27 
 import org.xml.sax.SAXException;
 28 
 import org.xml.sax.XMLReader;
 29 
 import org.xml.sax.helpers.XMLReaderFactory;
 30 
 
 31 Block: 1/1 
 public class RequestBroker
 32 
 {
 33 
 	final String UTF_8 = "UTF-8";
 34 
 	final int TIMEOUT_MSEC = 5000;	// Allow at most 5 seconds before we declare the server to be unresponsive
 35 
 	
 36 
 	String m_gatewayUrl;
 37 
 	ArrayList<NameValuePair> m_baseParams;
 38 
 
 39 
 	public RequestBroker(String gatewayUrl, ArrayList<NameValuePair> baseParams)
 40 Block: 1/1 
 	{
 41 
 		m_gatewayUrl = gatewayUrl;
 42 
 		m_baseParams = new ArrayList<NameValuePair>(baseParams);
 43 
 	}
 44 
 	
 45 
 	// NB:  Caller is responsible for close()ing the returned InputStream
 46 
 	//
 47 
 	InputStream doSubmit(String method, ArrayList<NameValuePair> passedParams) throws QDException
 48 
 	{
 49 Block: 0/1 
 		ArrayList<NameValuePair> params = new ArrayList<NameValuePair>(m_baseParams);
 50 
 		params.addAll(passedParams);
 51 
 		
 52 
 		try {
 53 
 			RequestConfig.Builder requestBuilder = RequestConfig.custom();
 54 
 			requestBuilder = requestBuilder.setConnectTimeout(TIMEOUT_MSEC);
 55 
 			requestBuilder = requestBuilder.setSocketTimeout(TIMEOUT_MSEC);
 56 
 			HttpClientBuilder builder = HttpClientBuilder.create();
 57 
 			builder.setDefaultRequestConfig(requestBuilder.build());
 58 
 			HttpClient httpClient = builder.build(); 
 59 
 			HttpPost httpPost = new HttpPost(m_gatewayUrl + "/" + method + "/");
 60 
 			httpPost.setEntity(new UrlEncodedFormEntity(params, UTF_8));
 61 
 			HttpResponse response = httpClient.execute(httpPost);
 62 
 			HttpEntity entity = response.getEntity();
 63 
 
 64 
 			if (null != entity) {
 65 Block: 0/1 Branch: 0/1 
 				InputStream is = entity.getContent();
 66 
 				return is;
 67 
 			}
 68 
 		}
 69 Block: 0/1 
 		catch (UnsupportedEncodingException uee) {
 70 
 			// We should actually be guaranteed that this never happens, 
 71 
 			// because all JVMs are required to support UTF-8
 72 
 			assert(false);
 73 Block: 0/1 Branch: 0/1 
 			throw new Error(uee);
 74 
 		}
 75 Block: 0/1 
 		catch (IOException ioe) {
 76 
 			throw new QDException(ioe);
 77 Block: 0/1 Branch: 0/1 
 		}
 78 
 		
 79 Block: 0/1 
 		return null;
 80 
 	}
 81 
 
 82 
 	public String submit(String method, ArrayList<NameValuePair> passedParams) throws QDException
 83 
 	{
 84 Block: 1/1 
 		StringBuilder sb = new StringBuilder();
 85 
 		InputStream is = null;
 86 
 
 87 
 		try {
 88 
 			is = doSubmit(method, passedParams);
 89 
 			BufferedReader br = new BufferedReader(new InputStreamReader(is));
 90 
 			String line = br.readLine();
 91 Block: 1/1 
 			while (null != line) {
 92 Block: 1/1 Branch: 1/1 
 				sb.append(line).append("\n");
 93 
 				line = br.readLine();
 94 
 			}
 95 
 		}
 96 Block: 0/1 
 		catch (IOException ioe) {
 97 
 			throw new QDException(ioe);
 98 
 		}
 99 
 		finally {
 100 Block: 1/2 Branch: 1/1 
 			ExceptionUtils.tryClose(is);
 101 
 		}
 102 
 
 103 Block: 1/1 
 		return sb.toString();
 104 
 	}
 105 
 	
 106 
 	public ParseResult submitAndParse(String method, 
 107 
 			  ArrayList<NameValuePair> passedParams, 
 108 
 			  Class<? extends ParseResult> rootParserClass)
 109 
 	throws QDException
 110 
 	{
 111 Block: 1/1 
 		return submitAndParse(method, passedParams, rootParserClass, null);
 112 
 	}
 113 
 	
 114 
 	public ParseResult submitAndParse(String method, 
 115 
 									  ArrayList<NameValuePair> passedParams, 
 116 
 									  Class<? extends ParseResult> rootParserClass,
 117 
 									  String rootTagName)
 118 
 	throws QDException
 119 
 	{
 120 Block: 1/1 
 		ParseResult result = null;
 121 
 		InputStream is = null;
 122 
 		
 123 
 		try {
 124 
 			if (null == rootTagName) {
 125 Block: 1/1 Branch: 1/1 
 				result = (ParseResult) rootParserClass.newInstance();
 126 
 			} 
 127 
 			else {
 128 Block: 0/1 Branch: 0/1 
 				result = (ParseResult) rootParserClass.getDeclaredConstructor(String.class).newInstance(rootTagName);
 129 
 			}
 130 Block: 1/1 
 			is = doSubmit(method, passedParams);
 131 
 			XMLReader reader = XMLReaderFactory.createXMLReader();
 132 
 			ParseHandler ph = new ParseHandler(result);
 133 
 			reader.setContentHandler(ph);
 134 
 			reader.parse(new InputSource(is));
 135 
 		} 
 136 Block: 0/1 
 		catch ( InstantiationException
 137 
 				| InvocationTargetException
 138 
 				| IllegalAccessException
 139 
 				| IOException 
 140 
 				| NoSuchMethodException
 141 
 				| SAXException 
 142 
 				e )
 143 
 		{
 144 
 			throw new QDException(e);
 145 
 		} 
 146 
 		finally {
 147 Block: 0/1 
 			ExceptionUtils.tryClose(is);
 148 
 		}
 149 
 		
 150 Block: 1/1 
 		return result;
 151 
 	}
 152 
 }
-

-

Report generated 11/12/14 11:31 PM
- -