@Test public void testRequestURL() throws Exception { URLStreamProvider streamProvider = createNiceMock(URLStreamProvider.class); HttpURLConnection urlConnection = createNiceMock(HttpURLConnection.class); ViewContext viewContext = createNiceMock(ViewContext.class); String responseBody = "Response body..."; InputStream inputStream = new ByteArrayInputStream(responseBody.getBytes(Charset.forName("UTF-8"))); expect(streamProvider.processURL(eq("spec?doAs=joe"), eq("requestMethod"), eq((String) null), eq((Map<String, List<String>>)null))).andReturn(urlConnection); expect(urlConnection.getInputStream()).andReturn(inputStream); expect(viewContext.getUsername()).andReturn("joe").anyTimes(); replay(streamProvider, urlConnection, viewContext); HttpImpersonatorImpl impersonator = new HttpImpersonatorImpl(viewContext, streamProvider); ImpersonatorSetting setting = new ImpersonatorSettingImpl(viewContext); Assert.assertEquals(responseBody, impersonator.requestURL("spec", "requestMethod", setting)); verify(streamProvider, urlConnection, viewContext); }
@Override public String requestURL(String url, String requestType, final ImpersonatorSetting impersonatorSetting) { String result = ""; BufferedReader rd; String line; if (url.toLowerCase().contains(impersonatorSetting.getDoAsParamName().toLowerCase())) { throw new IllegalArgumentException("URL cannot contain \"" + impersonatorSetting.getDoAsParamName() + "\" parameter"); } try { String username = impersonatorSetting.getUsername(); if (username != null) { URIBuilder builder = new URIBuilder(url); builder.addParameter(impersonatorSetting.getDoAsParamName(), username); url = builder.build().toString(); } HttpURLConnection connection = urlStreamProvider.processURL(url, requestType, (String) null, null); int responseCode = connection.getResponseCode(); InputStream resultInputStream; if (responseCode >= ProxyService.HTTP_ERROR_RANGE_START) { resultInputStream = connection.getErrorStream(); } else { resultInputStream = connection.getInputStream(); } rd = new BufferedReader(new InputStreamReader(resultInputStream)); if (rd != null) { line = rd.readLine(); while (line != null) { result += line; line = rd.readLine(); } rd.close(); } } catch (Exception e) { LOG.error("Exception caught processing impersonator request.", e); } return result; }
HttpImpersonatorImpl implements HttpImpersonator { @Override public String requestURL(String url, String requestType, final ImpersonatorSetting impersonatorSetting) { String result = ""; BufferedReader rd; String line; if (url.toLowerCase().contains(impersonatorSetting.getDoAsParamName().toLowerCase())) { throw new IllegalArgumentException("URL cannot contain \"" + impersonatorSetting.getDoAsParamName() + "\" parameter"); } try { String username = impersonatorSetting.getUsername(); if (username != null) { URIBuilder builder = new URIBuilder(url); builder.addParameter(impersonatorSetting.getDoAsParamName(), username); url = builder.build().toString(); } HttpURLConnection connection = urlStreamProvider.processURL(url, requestType, (String) null, null); int responseCode = connection.getResponseCode(); InputStream resultInputStream; if (responseCode >= ProxyService.HTTP_ERROR_RANGE_START) { resultInputStream = connection.getErrorStream(); } else { resultInputStream = connection.getInputStream(); } rd = new BufferedReader(new InputStreamReader(resultInputStream)); if (rd != null) { line = rd.readLine(); while (line != null) { result += line; line = rd.readLine(); } rd.close(); } } catch (Exception e) { LOG.error("Exception caught processing impersonator request.", e); } return result; } }
HttpImpersonatorImpl implements HttpImpersonator { @Override public String requestURL(String url, String requestType, final ImpersonatorSetting impersonatorSetting) { String result = ""; BufferedReader rd; String line; if (url.toLowerCase().contains(impersonatorSetting.getDoAsParamName().toLowerCase())) { throw new IllegalArgumentException("URL cannot contain \"" + impersonatorSetting.getDoAsParamName() + "\" parameter"); } try { String username = impersonatorSetting.getUsername(); if (username != null) { URIBuilder builder = new URIBuilder(url); builder.addParameter(impersonatorSetting.getDoAsParamName(), username); url = builder.build().toString(); } HttpURLConnection connection = urlStreamProvider.processURL(url, requestType, (String) null, null); int responseCode = connection.getResponseCode(); InputStream resultInputStream; if (responseCode >= ProxyService.HTTP_ERROR_RANGE_START) { resultInputStream = connection.getErrorStream(); } else { resultInputStream = connection.getInputStream(); } rd = new BufferedReader(new InputStreamReader(resultInputStream)); if (rd != null) { line = rd.readLine(); while (line != null) { result += line; line = rd.readLine(); } rd.close(); } } catch (Exception e) { LOG.error("Exception caught processing impersonator request.", e); } return result; } HttpImpersonatorImpl(ViewContext c);  HttpImpersonatorImpl(ViewContext c, URLStreamProvider urlStreamProvider); }
HttpImpersonatorImpl implements HttpImpersonator { @Override public String requestURL(String url, String requestType, final ImpersonatorSetting impersonatorSetting) { String result = ""; BufferedReader rd; String line; if (url.toLowerCase().contains(impersonatorSetting.getDoAsParamName().toLowerCase())) { throw new IllegalArgumentException("URL cannot contain \"" + impersonatorSetting.getDoAsParamName() + "\" parameter"); } try { String username = impersonatorSetting.getUsername(); if (username != null) { URIBuilder builder = new URIBuilder(url); builder.addParameter(impersonatorSetting.getDoAsParamName(), username); url = builder.build().toString(); } HttpURLConnection connection = urlStreamProvider.processURL(url, requestType, (String) null, null); int responseCode = connection.getResponseCode(); InputStream resultInputStream; if (responseCode >= ProxyService.HTTP_ERROR_RANGE_START) { resultInputStream = connection.getErrorStream(); } else { resultInputStream = connection.getInputStream(); } rd = new BufferedReader(new InputStreamReader(resultInputStream)); if (rd != null) { line = rd.readLine(); while (line != null) { result += line; line = rd.readLine(); } rd.close(); } } catch (Exception e) { LOG.error("Exception caught processing impersonator request.", e); } return result; } HttpImpersonatorImpl(ViewContext c);  HttpImpersonatorImpl(ViewContext c, URLStreamProvider urlStreamProvider); ViewContext getContext(); String getUsername(); @Override HttpURLConnection doAs(HttpURLConnection conn, String type); @Override HttpURLConnection doAs(HttpURLConnection conn, String type, String username, String doAsParamName); @Override String requestURL(String url, String requestType, final ImpersonatorSetting impersonatorSetting); }
HttpImpersonatorImpl implements HttpImpersonator { @Override public String requestURL(String url, String requestType, final ImpersonatorSetting impersonatorSetting) { String result = ""; BufferedReader rd; String line; if (url.toLowerCase().contains(impersonatorSetting.getDoAsParamName().toLowerCase())) { throw new IllegalArgumentException("URL cannot contain \"" + impersonatorSetting.getDoAsParamName() + "\" parameter"); } try { String username = impersonatorSetting.getUsername(); if (username != null) { URIBuilder builder = new URIBuilder(url); builder.addParameter(impersonatorSetting.getDoAsParamName(), username); url = builder.build().toString(); } HttpURLConnection connection = urlStreamProvider.processURL(url, requestType, (String) null, null); int responseCode = connection.getResponseCode(); InputStream resultInputStream; if (responseCode >= ProxyService.HTTP_ERROR_RANGE_START) { resultInputStream = connection.getErrorStream(); } else { resultInputStream = connection.getInputStream(); } rd = new BufferedReader(new InputStreamReader(resultInputStream)); if (rd != null) { line = rd.readLine(); while (line != null) { result += line; line = rd.readLine(); } rd.close(); } } catch (Exception e) { LOG.error("Exception caught processing impersonator request.", e); } return result; } HttpImpersonatorImpl(ViewContext c);  HttpImpersonatorImpl(ViewContext c, URLStreamProvider urlStreamProvider); ViewContext getContext(); String getUsername(); @Override HttpURLConnection doAs(HttpURLConnection conn, String type); @Override HttpURLConnection doAs(HttpURLConnection conn, String type, String username, String doAsParamName); @Override String requestURL(String url, String requestType, final ImpersonatorSetting impersonatorSetting);  }