Search in sources :

Example 6 with ContentTypeAwareResponse

use of com.thoughtworks.go.server.newsecurity.models.ContentTypeAwareResponse in project gocd by gocd.

the class UserEnabledCheckFilterForApiRequest method handleFailure.

@Override
void handleFailure(HttpServletRequest request, HttpServletResponse response, String errorMessage) throws IOException {
    response.setStatus(401);
    final ContentTypeAwareResponse contentTypeAwareResponse = CONTENT_TYPE_NEGOTIATION_MESSAGE_HANDLER.getResponse(request);
    response.setCharacterEncoding("utf-8");
    response.setContentType(contentTypeAwareResponse.getContentType().toString());
    response.getOutputStream().print(contentTypeAwareResponse.getFormattedMessage(errorMessage));
}
Also used : ContentTypeAwareResponse(com.thoughtworks.go.server.newsecurity.models.ContentTypeAwareResponse)

Example 7 with ContentTypeAwareResponse

use of com.thoughtworks.go.server.newsecurity.models.ContentTypeAwareResponse in project gocd by gocd.

the class BasicAuthenticationWithChallengeFailureResponseHandler method handle.

@Override
public void handle(HttpServletRequest request, HttpServletResponse response, int statusCode, String errorMessage) throws IOException {
    if (!isAjaxRequest(request)) {
        if (securityService.isSecurityEnabled()) {
            response.addHeader("WWW-Authenticate", "Basic realm=\"GoCD\"");
        }
    }
    response.setStatus(statusCode);
    final ContentTypeAwareResponse contentTypeAwareResponse = CONTENT_TYPE_NEGOTIATION_MESSAGE_HANDLER.getResponse(request);
    response.setCharacterEncoding("utf-8");
    response.setContentType(contentTypeAwareResponse.getContentType().toString());
    response.getOutputStream().print(contentTypeAwareResponse.getFormattedMessage(errorMessage));
}
Also used : ContentTypeAwareResponse(com.thoughtworks.go.server.newsecurity.models.ContentTypeAwareResponse)

Example 8 with ContentTypeAwareResponse

use of com.thoughtworks.go.server.newsecurity.models.ContentTypeAwareResponse in project gocd by gocd.

the class RequestRejectedExceptionHandler method handle.

public void handle(HttpServletRequest request, HttpServletResponse response, String message, HttpStatus httpStatus) throws IOException {
    if (API_REQUEST_MATCHER.matches(request)) {
        response.setStatus(HttpStatus.BAD_REQUEST.value());
        final ContentTypeAwareResponse contentTypeAwareResponse = CONTENT_TYPE_NEGOTIATION_MESSAGE_HANDLER.getResponse(request);
        response.setCharacterEncoding("utf-8");
        response.setContentType(contentTypeAwareResponse.getContentType().toString());
        response.getOutputStream().print(contentTypeAwareResponse.getFormattedMessage(message));
    } else {
        response.sendError(httpStatus.value(), message);
    }
}
Also used : ContentTypeAwareResponse(com.thoughtworks.go.server.newsecurity.models.ContentTypeAwareResponse)

Example 9 with ContentTypeAwareResponse

use of com.thoughtworks.go.server.newsecurity.models.ContentTypeAwareResponse in project gocd by gocd.

the class ContentTypeNegotiationMessageRendererTest method shouldGenerateXMLResponseMessageForContentType.

@ParameterizedTest
@ValueSource(strings = { MediaType.TEXT_XML_VALUE, MediaType.APPLICATION_XML_VALUE })
void shouldGenerateXMLResponseMessageForContentType(String contentType) {
    final MockHttpServletRequest request = HttpRequestBuilder.GET("/").withHeader("Accept", contentType).build();
    final ContentTypeAwareResponse response = new ContentTypeNegotiationMessageRenderer().getResponse(request);
    assertThat(response.getContentType().toString()).isEqualTo(contentType);
    assertThat(response.getFormattedMessage("foo")).isEqualTo("<access-denied>\n  <message>foo</message>\n</access-denied>\n");
}
Also used : ContentTypeAwareResponse(com.thoughtworks.go.server.newsecurity.models.ContentTypeAwareResponse) MockHttpServletRequest(com.thoughtworks.go.http.mocks.MockHttpServletRequest) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 10 with ContentTypeAwareResponse

use of com.thoughtworks.go.server.newsecurity.models.ContentTypeAwareResponse in project gocd by gocd.

the class ContentTypeNegotiationMessageRendererTest method shouldJsonResponseMessageWhenRequestIsMadeWithoutContentTypeAndItIsNotForXMLFile.

@Test
void shouldJsonResponseMessageWhenRequestIsMadeWithoutContentTypeAndItIsNotForXMLFile() {
    final MockHttpServletRequest request = HttpRequestBuilder.GET("/random").build();
    final ContentTypeAwareResponse response = new ContentTypeNegotiationMessageRenderer().getResponse(request);
    assertThat(response.getContentType().toString()).isEqualTo("application/json");
    assertThat(response.getFormattedMessage("foo")).isEqualTo("{\n  \"message\": \"foo\"\n}");
}
Also used : ContentTypeAwareResponse(com.thoughtworks.go.server.newsecurity.models.ContentTypeAwareResponse) MockHttpServletRequest(com.thoughtworks.go.http.mocks.MockHttpServletRequest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

ContentTypeAwareResponse (com.thoughtworks.go.server.newsecurity.models.ContentTypeAwareResponse)10 MockHttpServletRequest (com.thoughtworks.go.http.mocks.MockHttpServletRequest)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 Test (org.junit.jupiter.api.Test)2 ValueSource (org.junit.jupiter.params.provider.ValueSource)2 ContentTypeNegotiationMessageRenderer (com.thoughtworks.go.server.newsecurity.handlers.renderer.ContentTypeNegotiationMessageRenderer)1 MediaType (org.springframework.http.MediaType)1