/** * Executes a request, returning the response as JSON to be handled by makeRequest. */ public void fetch(HttpServletRequest request, HttpServletResponse response) throws GadgetException, IOException { HttpRequest rcr = buildHttpRequest(request); // Serialize the response HttpResponse results = requestPipeline.execute(rcr); // Rewrite the response if (contentRewriterRegistry != null) { try { results = contentRewriterRegistry.rewriteHttpResponse(rcr, results); } catch (RewritingException e) { throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e, e.getHttpStatusCode()); } } // Serialize the response String output = convertResponseToJson(rcr.getSecurityToken(), request, results); // Find and set the refresh interval setResponseHeaders(request, response, results); response.setStatus(HttpServletResponse.SC_OK); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(UNPARSEABLE_CRUFT + output); }
/** * Executes a request, returning the response as JSON to be handled by makeRequest. */ public void fetch(HttpServletRequest request, HttpServletResponse response) throws GadgetException, IOException { HttpRequest rcr = buildHttpRequest(request); // Serialize the response HttpResponse results = requestPipeline.execute(rcr); // Rewrite the response if (contentRewriterRegistry != null) { try { results = contentRewriterRegistry.rewriteHttpResponse(rcr, results); } catch (RewritingException e) { throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e, e.getHttpStatusCode()); } } // Serialize the response String output = convertResponseToJson(rcr.getSecurityToken(), request, results); // Find and set the refresh interval setResponseHeaders(request, response, results); response.setStatus(HttpServletResponse.SC_OK); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(UNPARSEABLE_CRUFT + output); }
setResponseHeaders(request, response, results); response.setStatus(HttpServletResponse.SC_OK); response.setCharacterEncoding("UTF-8");
@Test public void testSetResponseHeadersForceParamInvalid() throws Exception { HttpResponse results = new HttpResponseBuilder().create(); expect(request.getParameter(Param.REFRESH.getKey())).andReturn("foo").anyTimes(); replay(); try { MakeRequestHandler.setResponseHeaders(request, recorder, results); } catch (GadgetException e) { assertEquals(GadgetException.Code.INVALID_PARAMETER, e.getCode()); } }
@Test public void testSetResponseHeadersForceParam() throws Exception { HttpResponse results = new HttpResponseBuilder().create(); expect(request.getParameter(Param.REFRESH.getKey())).andReturn("30").anyTimes(); replay(); // not sure why but the following line seems to help this test past deterministically System.out.println("request started at " + HttpUtilTest.testStartTime); handler.setResponseHeaders(request, recorder, results); HttpUtilTest.checkCacheControlHeaders(HttpUtilTest.testStartTime, recorder, 30, false); assertEquals("attachment;filename=p.txt", recorder.getHeader("Content-Disposition")); }
@Test public void testSetResponseHeadersForceParam() throws Exception { HttpResponse results = new HttpResponseBuilder().create(); expect(request.getParameter(Param.REFRESH.getKey())).andReturn("30").anyTimes(); replay(); // not sure why but the following line seems to help this test past deterministically System.out.println("request started at " + HttpUtilTest.testStartTime); handler.setResponseHeaders(request, recorder, results); HttpUtilTest.checkCacheControlHeaders(HttpUtilTest.testStartTime, recorder, 30, false); assertEquals("attachment;filename=p.txt", recorder.getHeader("Content-Disposition")); }
@Test public void testSetResponseHeadersForceParam() throws Exception { HttpResponse results = new HttpResponseBuilder().create(); expect(request.getParameter(Param.REFRESH.getKey())).andReturn("30").anyTimes(); replay(); // not sure why but the following line seems to help this test past deterministically System.out.println("request started at " + HttpUtilTest.testStartTime); MakeRequestHandler.setResponseHeaders(request, recorder, results); HttpUtilTest.checkCacheControlHeaders(HttpUtilTest.testStartTime, recorder, 30, false); assertEquals("attachment;filename=p.txt", recorder.getHeader("Content-Disposition")); }
@Test public void testSetResponseHeadersForceParamInvalid() throws Exception { HttpResponse results = new HttpResponseBuilder().create(); expect(request.getParameter(Param.REFRESH.getKey())).andReturn("foo").anyTimes(); replay(); try { handler.setResponseHeaders(request, recorder, results); } catch (GadgetException e) { assertEquals(GadgetException.Code.INVALID_PARAMETER, e.getCode()); } }
@Test public void testSetResponseHeadersForceParamInvalid() throws Exception { HttpResponse results = new HttpResponseBuilder().create(); expect(request.getParameter(Param.REFRESH.getKey())).andReturn("foo").anyTimes(); replay(); try { handler.setResponseHeaders(request, recorder, results); } catch (GadgetException e) { assertEquals(GadgetException.Code.INVALID_PARAMETER, e.getCode()); } }
@Test public void testSetContentTypeHeader() throws Exception { HttpResponse results = new HttpResponseBuilder() .create(); replay(); handler.setResponseHeaders(request, recorder, results); assertEquals("application/octet-stream", recorder.getHeader("Content-Type")); }
@Test public void testSetContentTypeHeader() throws Exception { HttpResponse results = new HttpResponseBuilder() .create(); replay(); MakeRequestHandler.setResponseHeaders(request, recorder, results); assertEquals("application/octet-stream", recorder.getHeader("Content-Type")); }
@Test public void testSetContentTypeHeader() throws Exception { HttpResponse results = new HttpResponseBuilder() .create(); replay(); handler.setResponseHeaders(request, recorder, results); assertEquals("application/octet-stream", recorder.getHeader("Content-Type")); }
@Test public void testSetResponseHeadersNoCache() throws Exception { Map<String, List<String>> headers = new TreeMap<String,List<String>>(String.CASE_INSENSITIVE_ORDER); headers.put("Pragma", Arrays.asList("no-cache")); HttpResponse results = new HttpResponseBuilder() .addHeader("Pragma", "no-cache") .create(); replay(); handler.setResponseHeaders(request, recorder, results); // Just verify that they were set. Specific values are configurable. assertNotNull("Expires header not set", recorder.getHeader("Expires")); assertEquals("no-cache", recorder.getHeader("Pragma")); assertEquals("no-cache", recorder.getHeader("Cache-Control")); assertEquals("attachment;filename=p.txt", recorder.getHeader("Content-Disposition")); }
@Test public void testSetResponseHeadersNoCache() throws Exception { Map<String, List<String>> headers = new TreeMap<String,List<String>>(String.CASE_INSENSITIVE_ORDER); headers.put("Pragma", Arrays.asList("no-cache")); HttpResponse results = new HttpResponseBuilder() .addHeader("Pragma", "no-cache") .create(); replay(); handler.setResponseHeaders(request, recorder, results); // Just verify that they were set. Specific values are configurable. assertNotNull("Expires header not set", recorder.getHeader("Expires")); assertEquals("no-cache", recorder.getHeader("Pragma")); assertEquals("no-cache", recorder.getHeader("Cache-Control")); assertEquals("attachment;filename=p.txt", recorder.getHeader("Content-Disposition")); }
@Test public void testSetResponseHeadersNoCache() throws Exception { Map<String, List<String>> headers = Maps.newTreeMap(String.CASE_INSENSITIVE_ORDER); headers.put("Pragma", Arrays.asList("no-cache")); HttpResponse results = new HttpResponseBuilder() .addHeader("Pragma", "no-cache") .create(); replay(); MakeRequestHandler.setResponseHeaders(request, recorder, results); // Just verify that they were set. Specific values are configurable. assertNotNull("Expires header not set", recorder.getHeader("Expires")); assertEquals("no-cache", recorder.getHeader("Pragma")); assertEquals("no-cache", recorder.getHeader("Cache-Control")); assertEquals("attachment;filename=p.txt", recorder.getHeader("Content-Disposition")); }
@Test public void testSetResponseHeaders() throws Exception { HttpResponse results = new HttpResponseBuilder().create(); replay(); handler.setResponseHeaders(request, recorder, results); // Just verify that they were set. Specific values are configurable. assertNotNull("Expires header not set", recorder.getHeader("Expires")); assertNotNull("Cache-Control header not set", recorder.getHeader("Cache-Control")); assertEquals("attachment;filename=p.txt", recorder.getHeader("Content-Disposition")); }
@Test public void testSetResponseHeaders() throws Exception { HttpResponse results = new HttpResponseBuilder().create(); replay(); MakeRequestHandler.setResponseHeaders(request, recorder, results); // Just verify that they were set. Specific values are configurable. assertNotNull("Expires header not set", recorder.getHeader("Expires")); assertNotNull("Cache-Control header not set", recorder.getHeader("Cache-Control")); assertEquals("attachment;filename=p.txt", recorder.getHeader("Content-Disposition")); }
@Test public void testSetResponseHeaders() throws Exception { HttpResponse results = new HttpResponseBuilder().create(); replay(); handler.setResponseHeaders(request, recorder, results); // Just verify that they were set. Specific values are configurable. assertNotNull("Expires header not set", recorder.getHeader("Expires")); assertNotNull("Cache-Control header not set", recorder.getHeader("Cache-Control")); assertEquals("attachment;filename=p.txt", recorder.getHeader("Content-Disposition")); }