public void intercept(InterceptorStack stack, ResourceMethod method, Object resourceInstance)
      throws InterceptionException {
    Consumes consumesAnnotation = method.getMethod().getAnnotation(Consumes.class);
    List<String> supported = Arrays.asList(consumesAnnotation.value());

    String contentType = request.getContentType();
    if (!supported.isEmpty() && !supported.contains(contentType)) {
      unsupported(
          String.format(
              "Request with media type [%s]. Expecting one of %s.", contentType, supported));
      return;
    }

    try {
      Deserializer deserializer = deserializers.deserializerFor(contentType, container);
      if (deserializer == null) {
        unsupported(
            String.format("Unable to handle media type [%s]: no deserializer found.", contentType));
        return;
      }

      Object[] deserialized = deserializer.deserialize(request.getInputStream(), method);
      Object[] parameters = methodInfo.getParameters();

      for (int i = 0; i < deserialized.length; i++) {
        if (deserialized[i] != null) {
          parameters[i] = deserialized[i];
        }
      }

      stack.next(method, resourceInstance);
    } catch (IOException e) {
      throw new InterceptionException(e);
    }
  }
  @Test
  public void whenResultIsAnInputStreamShouldCreateAByteArrayDownload() throws Exception {
    byte[] bytes = "abc".getBytes();
    when(info.getResult()).thenReturn(bytes);

    interceptor.intercept(stack, controllerMethod, null);

    verify(outputStream).write(argThat(is(arrayStartingWith(bytes))), eq(0), eq(3));
  }
  @Test
  public void whenResultIsADownloadShouldUseIt() throws Exception {
    Download download = mock(Download.class);

    when(info.getResult()).thenReturn(download);

    interceptor.intercept(stack, controllerMethod, null);

    verify(download).write(response);
  }
  @Test
  public void whenResultIsNullAndResultWasUsedShouldDoNothing() throws Exception {
    when(info.getResult()).thenReturn(null);
    when(result.used()).thenReturn(true);

    interceptor.intercept(stack, controllerMethod, null);

    verify(stack).next(controllerMethod, null);
    verifyZeroInteractions(response);
  }
Beispiel #5
0
  public void outject(@Observes MethodExecuted event, Result result, MethodInfo methodInfo) {

    Type returnType = event.getMethodReturnType();

    if (!returnType.equals(Void.TYPE)) {
      String name = extractor.nameFor(returnType);
      Object value = methodInfo.getResult();
      logger.debug("outjecting {}={}", name, value);
      result.include(name, value);
    }
  }
  @Test
  public void whenResultIsNullAndResultWasNotUsedShouldThrowNPE() throws Exception {
    when(info.getResult()).thenReturn(null);
    when(result.used()).thenReturn(false);

    try {
      interceptor.intercept(stack, controllerMethod, null);
      fail("expected NullPointerException");
    } catch (NullPointerException e) {
      verifyZeroInteractions(response);
    }
  }
  @Test
  public void whenResultIsAFileShouldCreateAFileDownload() throws Exception {
    File tmp = File.createTempFile("test", "test");
    Files.write(tmp.toPath(), "abc".getBytes());

    when(info.getResult()).thenReturn(tmp);

    interceptor.intercept(stack, controllerMethod, null);
    verify(outputStream).write(argThat(is(arrayStartingWith("abc".getBytes()))), eq(0), eq(3));

    tmp.delete();
  }
  @Test
  public void shouldThrowInterceptionExceptionIfIOExceptionOccurs() throws Exception {
    Download download = mock(Download.class);

    when(info.getResult()).thenReturn(download);
    when(result.used()).thenReturn(false);
    doThrow(new IOException()).when(download).write(any(HttpServletResponse.class));

    try {
      interceptor.intercept(stack, controllerMethod, null);
      fail("expected InterceptionException");
    } catch (InterceptionException e) {

    }
  }
  public void intercept(
      InterceptorStack stack, ResourceMethod ignorableMethod, Object resourceInstance)
      throws InterceptionException {

    try {
      ResourceMethod method = translator.translate(requestInfo);

      methodInfo.setResourceMethod(method);
      stack.next(method, resourceInstance);
    } catch (ResourceNotFoundException e) {
      resourceNotFoundHandler.couldntFind(requestInfo);
    } catch (MethodNotAllowedException e) {
      LOGGER.debug(e.getMessage(), e);
      methodNotAllowedHandler.deny(requestInfo, e.getAllowedMethods());
    }
  }