Example usage for org.springframework.http HttpHeaders ACCEPT

List of usage examples for org.springframework.http HttpHeaders ACCEPT

Introduction

In this page you can find the example usage for org.springframework.http HttpHeaders ACCEPT.

Prototype

String ACCEPT

To view the source code for org.springframework.http HttpHeaders ACCEPT.

Click Source Link

Document

The HTTP Accept header field name.

Usage

From source file:web.rufer.swisscom.sms.api.factory.HeaderFactory.java

public static HttpHeaders createHeaders(String apiKey) {
    HttpHeaders headers = new HttpHeaders();
    headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
    headers.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
    headers.set(CLIENT_ID, apiKey);//from  ww  w.j a v a2 s  .co  m
    return headers;
}

From source file:nl.dtls.fairdatapoint.api.config.ApplicationFilter.java

@Override
public void doFilter(ServletRequest sr, ServletResponse sr1, FilterChain fc)
        throws IOException, ServletException {
    HttpServletResponse response = (HttpServletResponse) sr1;
    HttpServletRequest request = (HttpServletRequest) sr;
    response.setHeader(HttpHeaders.SERVER, "FAIR data point (JAVA)");
    response.setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "*");
    response.setHeader(HttpHeaders.ALLOW, (RequestMethod.GET.name()));
    response.setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, (HttpHeaders.ACCEPT));
    ThreadContext.put("ipAddress", request.getRemoteAddr());
    ThreadContext.put("responseStatus", String.valueOf(response.getStatus()));
    fc.doFilter(sr, sr1);//from   ww  w  .jav  a2s  . c o m
    ThreadContext.clearAll();
}

From source file:com.github.ljtfreitas.restify.http.spring.contract.metadata.reflection.SpringWebRequestMappingMetadata.java

private Optional<String> produces() {
    return Optional.ofNullable(mapping).map(m -> m.produces()).filter(c -> c.length >= 1)
            .map(h -> Arrays.stream(h).filter(c -> c != null && !c.isEmpty()).collect(Collectors.joining(", ")))
            .map(c -> HttpHeaders.ACCEPT + "=" + c);
}

From source file:org.mitre.openid.connect.web.UserInfoEndpoint.java

/**
 * Get information about the user as specified in the accessToken included in this request
 *///from   ww w .ja v a2s  .  c  om
@PreAuthorize("hasRole('ROLE_USER') and #oauth2.hasScope('" + SystemScopeService.OPENID_SCOPE + "')")
@RequestMapping(method = { RequestMethod.GET, RequestMethod.POST }, produces = {
        MediaType.APPLICATION_JSON_VALUE, UserInfoJWTView.JOSE_MEDIA_TYPE_VALUE })
public String getInfo(@RequestParam(value = "claims", required = false) String claimsRequestJsonString,
        @RequestHeader(value = HttpHeaders.ACCEPT, required = false) String acceptHeader,
        OAuth2Authentication auth, Model model) {

    if (auth == null) {
        logger.error("getInfo failed; no principal. Requester is not authorized.");
        model.addAttribute(HttpCodeView.CODE, HttpStatus.FORBIDDEN);
        return HttpCodeView.VIEWNAME;
    }

    String username = auth.getName();
    UserInfo userInfo = userInfoService.getByUsernameAndClientId(username,
            auth.getOAuth2Request().getClientId());

    if (userInfo == null) {
        logger.error("getInfo failed; user not found: " + username);
        model.addAttribute(HttpCodeView.CODE, HttpStatus.NOT_FOUND);
        return HttpCodeView.VIEWNAME;
    }

    model.addAttribute(UserInfoView.SCOPE, auth.getOAuth2Request().getScope());

    model.addAttribute(UserInfoView.AUTHORIZED_CLAIMS, auth.getOAuth2Request().getExtensions().get("claims"));

    if (!Strings.isNullOrEmpty(claimsRequestJsonString)) {
        model.addAttribute(UserInfoView.REQUESTED_CLAIMS, claimsRequestJsonString);
    }

    model.addAttribute(UserInfoView.USER_INFO, userInfo);

    // content negotiation

    // start off by seeing if the client has registered for a signed/encrypted JWT from here
    ClientDetailsEntity client = clientService.loadClientByClientId(auth.getOAuth2Request().getClientId());
    model.addAttribute(UserInfoJWTView.CLIENT, client);

    List<MediaType> mediaTypes = MediaType.parseMediaTypes(acceptHeader);
    MediaType.sortBySpecificityAndQuality(mediaTypes);

    if (client.getUserInfoSignedResponseAlg() != null || client.getUserInfoEncryptedResponseAlg() != null
            || client.getUserInfoEncryptedResponseEnc() != null) {
        // client has a preference, see if they ask for plain JSON specifically on this request
        for (MediaType m : mediaTypes) {
            if (!m.isWildcardType() && m.isCompatibleWith(UserInfoJWTView.JOSE_MEDIA_TYPE)) {
                return UserInfoJWTView.VIEWNAME;
            } else if (!m.isWildcardType() && m.isCompatibleWith(MediaType.APPLICATION_JSON)) {
                return UserInfoView.VIEWNAME;
            }
        }

        // otherwise return JWT
        return UserInfoJWTView.VIEWNAME;
    } else {
        // client has no preference, see if they asked for JWT specifically on this request
        for (MediaType m : mediaTypes) {
            if (!m.isWildcardType() && m.isCompatibleWith(MediaType.APPLICATION_JSON)) {
                return UserInfoView.VIEWNAME;
            } else if (!m.isWildcardType() && m.isCompatibleWith(UserInfoJWTView.JOSE_MEDIA_TYPE)) {
                return UserInfoJWTView.VIEWNAME;
            }
        }

        // otherwise return JSON
        return UserInfoView.VIEWNAME;
    }

}

From source file:example.users.UserControllerClientTests.java

private UserPayload issueGet(String path, MediaType mediaType) {

    HttpHeaders headers = new HttpHeaders();
    headers.add(HttpHeaders.ACCEPT, mediaType.toString());

    return template.exchange(path, HttpMethod.GET, new HttpEntity<Void>(headers), UserPayload.class).getBody();
}

From source file:org.zalando.stups.oauth2.spring.server.TokenInfoResourceServerTokenServicesTest.java

@Test
public void buildRequest() {

    RequestEntity<Void> entity = DefaultTokenInfoRequestExecutor.buildRequestEntity(URI.create(TOKENINFO_URL),
            "0123456789");

    Assertions.assertThat(entity).isNotNull();

    Assertions.assertThat(entity.getMethod()).isEqualTo(HttpMethod.GET);
    Assertions.assertThat(entity.getUrl()).isEqualTo(URI.create(TOKENINFO_URL));

    Assertions.assertThat(entity.getHeaders()).containsKey(HttpHeaders.AUTHORIZATION);
    List<String> authorizationHeader = entity.getHeaders().get(HttpHeaders.AUTHORIZATION);
    Assertions.assertThat(authorizationHeader).containsExactly("Bearer 0123456789");

    Assertions.assertThat(entity.getHeaders()).containsKey(HttpHeaders.ACCEPT);
    Assertions.assertThat(entity.getHeaders().getAccept()).contains(MediaType.APPLICATION_JSON);
}

From source file:example.company.UrlLevelSecurityTests.java

@Test
public void allowsGetRequestsButRejectsPostForUser() throws Exception {

    HttpHeaders headers = new HttpHeaders();
    headers.add(HttpHeaders.ACCEPT, MediaTypes.HAL_JSON.toString());
    headers.add(HttpHeaders.AUTHORIZATION, "Basic " + new String(Base64.encode(("greg:turnquist").getBytes())));

    mvc.perform(get("/employees").//
            headers(headers)).//
            andExpect(content().contentType(MediaTypes.HAL_JSON)).//
            andExpect(status().isOk()).//
            andDo(print());/*from   w w w  . j  av  a2s.  c o m*/

    mvc.perform(post("/employees").//
            headers(headers)).//
            andExpect(status().isForbidden()).//
            andDo(print());
}

From source file:example.company.UrlLevelSecurityTests.java

@Test
public void allowsPostRequestForAdmin() throws Exception {

    HttpHeaders headers = new HttpHeaders();
    headers.set(HttpHeaders.ACCEPT, "application/hal+json");
    headers.set(HttpHeaders.AUTHORIZATION, "Basic " + new String(Base64.encode(("ollie:gierke").getBytes())));

    mvc.perform(get("/employees").//
            headers(headers)).//
            andExpect(content().contentType(MediaTypes.HAL_JSON)).//
            andExpect(status().isOk()).//
            andDo(print());//from  ww w  . j  a  v  a  2s  .c o  m

    headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);

    String location = mvc.perform(post("/employees").//
            content(PAYLOAD).//
            headers(headers)).//
            andExpect(status().isCreated()).//
            andDo(print()).//
            andReturn().getResponse().getHeader(HttpHeaders.LOCATION);

    ObjectMapper mapper = new ObjectMapper();

    String content = mvc.perform(get(location)).//
            andReturn().getResponse().getContentAsString();
    Employee employee = mapper.readValue(content, Employee.class);

    assertThat(employee.getFirstName(), is("Saruman"));
    assertThat(employee.getLastName(), is("the White"));
    assertThat(employee.getTitle(), is("Wizard"));
}