示例#1
0
 @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 testHandleResponseValidArticleMessage() throws Exception {
   exc.setRequest(requestTB);
   exc.setResponse(Response.ok().body(getContent("/articleResponse.xml")).build());
   assertEquals(
       Outcome.CONTINUE, createValidatorInterceptor(ARTICLE_SERVICE_WSDL).handleResponse(exc));
 }
示例#3
0
 @Test
 public void testSkipFaults() throws Exception {
   ValidatorInterceptor i = createValidatorInterceptor(true);
   Exchange exc = createFaultExchange();
   assertEquals(Outcome.CONTINUE, i.handleResponse(exc));
   assertContains("secret", exc.getResponse().toString());
 }
示例#4
0
 private Exchange getExchangeCP(String path) throws IOException {
   Exchange exc = new Exchange(null);
   exc.setResponse(
       Response.ok()
           .contentType("text/xml")
           .body(getClass().getClassLoader().getResourceAsStream(path), true)
           .build());
   return exc;
 }
示例#5
0
 @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 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);
  }
  @Override
  public Outcome handleRequest(Exchange exc) throws Exception {
    if (exc.getRequest().isBodyEmpty()) {
      return Outcome.CONTINUE;
    }

    Case r = findRoute(exc.getRequest());
    if (r == null) {
      return Outcome.CONTINUE;
    }
    log.debug("match found for {" + r.getxPath() + "} routing to {" + r.getUrl() + "}");

    updateDestination(exc, r);
    return Outcome.CONTINUE;
  }
  @Test
  public void testRequest() throws Exception {
    HttpRouter r = new HttpRouter();
    r.setApplicationContext(applicationContext);
    when(applicationContext.getBean("abc")).thenReturn("OK");

    Exchange exc = new Exchange(null);
    exc.setRequest(new Request());

    GroovyInterceptor i = new GroovyInterceptor();
    i.setSrc("exc.setProperty('foo', 'bar')\n" + "def b = spring.getBean('abc')\n" + "CONTINUE");
    i.init(r);

    assertEquals(Outcome.CONTINUE, i.handleRequest(exc));
    assertEquals("bar", exc.getProperty("foo"));
    verify(applicationContext, times(1)).getBean("abc");
  }
  @Override
  public Outcome handleRequest(Exchange exc) throws Exception {
    System.out.println("MyInterceptor invoked.");

    System.out.println("Request headers:");
    for (HeaderField headerField : exc.getRequest().getHeader().getAllHeaderFields())
      System.out.print(headerField.getHeaderName() + ": " + headerField.getValue());

    return Outcome.CONTINUE;
  }
  @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());
  }
示例#11
0
  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());
  }
  public void rewriteEndpoint(Reader reader, Writer writer, int port, Exchange exc)
      throws XMLStreamException {
    XMLEventReader parser = inputFactory.createXMLEventReader(reader);
    XMLEventWriter eventWriter = XMLOutputFactory.newInstance().createXMLEventWriter(writer);

    String id = null;
    String url = null;

    skip:
    while (parser.hasNext()) {
      XMLEvent e = parser.nextEvent();

      if (e.isStartElement()) {
        if (isReplyTo(e.asStartElement())) {
          while (e.isStartElement() || !isReplyTo(e.asEndElement())) {
            if (e.isStartElement() && isAddress(e.asStartElement())) {
              url = parser.getElementText();
              addRewrittenAddressElement(eventWriter, url, port, e.asStartElement());

              continue skip;
            }

            eventWriter.add(e);
            e = parser.nextTag();
          }
        }

        if (isMessageId(e.asStartElement())) {
          id = parser.getElementText();
          exc.setProperty("messageId", id);
          addMessageIdElement(eventWriter, id, e.asStartElement());

          continue skip;
        }
      }

      eventWriter.add(e);
    }

    registry.register(id, url);
  }
示例#13
0
 public void collectStatisticsFrom(Exchange exc) {
   StatisticCollector sc = getStatisticCollectorByStatusCode(exc.getResponse().getStatusCode());
   synchronized (sc) {
     sc.collectFrom(exc);
   }
 }
 private Outcome getOutcome(Request request, Interceptor interceptor, String fileName)
     throws Exception {
   request.setBodyContent(getContent(fileName));
   exc.setRequest(request);
   return interceptor.handleRequest(exc);
 }
示例#15
0
 private Exchange createFaultExchange() {
   Exchange exc = new Exchange(null);
   exc.setResponse(HttpUtil.createSOAPValidationErrorResponse("secret"));
   return exc;
 }
 private void updateDestination(Exchange exc, Case r) {
   exc.setOriginalRequestUri(r.getUrl());
   exc.getDestinations().clear();
   exc.getDestinations().add(r.getUrl());
 }