public <T> T movedPermanentlyTo(final Class<T> controller) { return this.status.movedPermanentlyTo(controller); }
private void unsupported(String message) { this.status.unsupportedMediaType(message); }
@Post("/easySigin") public void signin(String email, String password) { boolean isAuthenticated = authenticator.authenticate(email, password); if (!isAuthenticated) { result.use(Results.status()).notAcceptable(); return; } result.use(Results.status()).accepted(); }
@Override public <T> Serializer from(T object, String alias) { if(object == null) { result.use(status()).notFound(); return new IgnoringSerializer(); } List<Serialization> serializations = newArrayList(this.serializations); sortSerializations(serializations); String format = formatResolver.getAcceptFormat(); for (Serialization serialization : serializations) { if (serialization.accepts(format)) { if(alias==null) { return serialization.from(object); } else { return serialization.from(object, alias); } } } result.use(status()).notAcceptable(); return new IgnoringSerializer(); }
private boolean analyzeImplementation(ResourceMethod method, ParameterizedType parameterized) { Type parameterType = parameterized.getActualTypeArguments()[0]; Class<?> found = (Class<?>) parameterType; T resource = retrieveResource(found); if(resource==null) { status.notFound(); return false; } if(allows(resource, method.getMethod())) { return true; } status.methodNotAllowed(allowedMethods()); return false; }
@Override public void notFound() { use(status()).notFound(); }
@Test public void whenThereIsNoFormatGivenShouldForwardToDefaultPage() { when(formatResolver.getAcceptFormat()).thenReturn(null); Serializer serializer = representation.from(new Object()); assertThat(serializer, is(instanceOf(IgnoringSerializer.class))); verify(status).notAcceptable(); }
@Test public void shouldSetMethodNotAllowedStatus() throws Exception { status.methodNotAllowed(EnumSet.of(HttpMethod.GET, HttpMethod.POST)); verify(response).sendError(405); verify(response).addHeader("Allow", "GET, POST"); }
@Test public void shouldSetAcceptedStatus() throws Exception { status.accepted(); verify(response).setStatus(202); }
@Test public void shouldSetHeader() throws Exception { status.header("Content-type", "application/xml"); verify(response).addHeader("Content-type", "application/xml"); }
@Test public void shouldSetConflictStatus() throws Exception { status.conflict(); verify(response).sendError(409); }
@Override public void onErrorSendBadRequest() { onErrorUse(status()).badRequest(getErrors()); }
@Test public void shouldSetNotFoundStatus() throws Exception { status.notFound(); verify(response).sendError(404); }
@Test public void onStatusResultShouldThrowExceptionsOnMethodNotAllowed() throws Exception { exception.expect(ValidationException.class); Status status = mock(Status.class); when(result.use(Status.class)).thenReturn(status); factory.instanceFor(Status.class, errors).methodNotAllowed(EnumSet.allOf(HttpMethod.class)); }
@Test public void onStatusResultShouldThrowExceptionsOnHeader() throws Exception { exception.expect(ValidationException.class); Status status = mock(Status.class); when(result.use(Status.class)).thenReturn(status); factory.instanceFor(Status.class, errors).header("abc", "def"); }
@Test public void onStatusResultShouldThrowExceptionsOnConflict() throws Exception { exception.expect(ValidationException.class); Status status = mock(Status.class); when(result.use(Status.class)).thenReturn(status); factory.instanceFor(Status.class, errors).conflict(); }
public void movedPermanentlyTo(String uri) { this.status.movedPermanentlyTo(uri); }
private void unsupported(String message, Status status, Object... params) { status.unsupportedMediaType(String.format(message, params)); } }
@Test public void shouldSend404IfNothingIsRendered() { when(formatResolver.getAcceptFormat()).thenReturn(null); Serializer serializer = representation.from(null); assertThat(serializer, is(instanceOf(IgnoringSerializer.class))); verify(status).notFound(); }
@Override @SuppressWarnings("unchecked") public <T> T permanentlyRedirectTo(T controller) { return (T) use(status()).movedPermanentlyTo(controller.getClass()); }