@Override public Outcome handleRequest(Exchange exc) throws Exception { Outcome outcome = exc.echo(); exc.getResponse().getHeader().removeFields(Header.CONTENT_LENGTH); String body = exc.getRequest().getUri() + "\n" + new String(exc.getRequest().getBody().getContent(), Constants.UTF_8_CHARSET); exc.getResponse().setBodyContent(body.getBytes(Constants.UTF_8_CHARSET)); return outcome; }
@Test public void testSkipFaults() throws Exception { ValidatorInterceptor i = createValidatorInterceptor(true); Exchange exc = createFaultExchange(); assertEquals(Outcome.CONTINUE, i.handleResponse(exc)); assertContains("secret", exc.getResponse().toString()); }
@Test public void testValidateFaults() throws Exception { ValidatorInterceptor i = createValidatorInterceptor(false); Exchange exc = createFaultExchange(); assertEquals(Outcome.ABORT, i.handleResponse(exc)); assertContainsNot("secret", exc.getResponse().toString()); }
@Test public void testProtocolSet() throws Exception { final ThrottleInterceptor i = new ThrottleInterceptor(); final Exchange exc = new Exchange(null); long t = System.currentTimeMillis(); i.handleRequest(exc); assertTrue(System.currentTimeMillis() - t < 2000); t = System.currentTimeMillis(); i.setDelay(3000); i.handleRequest(exc); assertTrue(System.currentTimeMillis() - t > 2000); i.setDelay(0); i.setMaxThreads(3); assertEquals(Outcome.CONTINUE, i.handleRequest(exc)); assertEquals(Outcome.ABORT, i.handleRequest(exc)); assertEquals(503, exc.getResponse().getStatusCode()); i.handleResponse(exc); assertEquals(Outcome.CONTINUE, i.handleRequest(exc)); i.setBusyDelay(3000); t = System.currentTimeMillis(); assertEquals(Outcome.ABORT, i.handleRequest(exc)); assertTrue(System.currentTimeMillis() - t > 2000); Thread thread1 = new Thread() { @Override public void run() { try { success = (Outcome.CONTINUE == i.handleRequest(exc)); } catch (Exception e) { throw new RuntimeException(e); } }; }; thread1.start(); Thread.sleep(1000); i.handleResponse(exc); thread1.join(); assertTrue(success); }
protected void rewrite(Exchange exc) throws Exception, IOException { log.debug("Changing endpoint address in WADL"); ByteArrayOutputStream stream = new ByteArrayOutputStream(); Relocator relocator = new Relocator( new OutputStreamWriter(stream, exc.getResponse().getCharset()), getLocationProtocol(), getLocationHost(exc), getLocationPort(exc), pathRewriter); relocator.getRelocatingAttributes().put(new QName(WADL_NS, "resources"), "base"); relocator.getRelocatingAttributes().put(new QName(WADL_NS, "include"), "href"); relocator.relocate( new InputStreamReader( exc.getResponse().getBodyAsStreamDecoded(), exc.getResponse().getCharset())); exc.getResponse().setBodyContent(stream.toByteArray()); }
@Test public void doit() throws Exception { Exchange exc = new Exchange(null); exc.setResponse( Response.ok().header("a", "b").header("c", "d").header("c", "d2").header("e", "f").build()); HeaderFilterInterceptor fhi = new HeaderFilterInterceptor(); fhi.setRules( Lists.newArrayList( new Rule[] { new Rule("Server", Action.REMOVE), // implicitly set by Response.ok() new Rule("a", Action.KEEP), new Rule("c.*", Action.REMOVE), })); fhi.handleResponse(exc); HeaderField[] h = exc.getResponse().getHeader().getAllHeaderFields(); assertEquals(3, h.length); assertEquals("Content-Length", h[0].getHeaderName().toString()); assertEquals("a", h[1].getHeaderName().toString()); assertEquals("e", h[2].getHeaderName().toString()); }
public void collectStatisticsFrom(Exchange exc) { StatisticCollector sc = getStatisticCollectorByStatusCode(exc.getResponse().getStatusCode()); synchronized (sc) { sc.collectFrom(exc); } }