Example usage for org.springframework.http HttpHeaders setContentType

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

Introduction

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

Prototype

public void setContentType(@Nullable MediaType mediaType) 

Source Link

Document

Set the MediaType media type of the body, as specified by the Content-Type header.

Usage

From source file:edu.fing.tagsi.db4o.business.TrackingController.java

public void RegistrarNuevoSalto(Envio envio, Recorrido recorrido, List<Tracking> list) {
    if (list.size() < recorrido.getNodos().length) {
        NodoCamino nodo = recorrido.getNodos()[list.size()];

        RestTemplate restTemplate = new RestTemplate();
        RequestTrackingAddPackage req = new RequestTrackingAddPackage(envio.getId().toString(),
                envio.getCliente().getId().toString(), nodo.getCiudad().getId().toString(), new Date(),
                list.size() + 1 == recorrido.getNodos().length);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<RequestTrackingAddPackage> entity = new HttpEntity<>(req, headers);
        restTemplate.postForObject(ConfigController.getInstance().getURLAddTracking(), entity, void.class);
    }//from   www.java 2s  . co  m

}

From source file:com.github.dactiv.fear.service.web.SystemCommonController.java

/**
 * ?????/*from   w w  w  . j  a  v  a  2 s  .c  o m*/
 *
 * @param token ??
 *
 * @return ?? byte 
 *
 * @throws IOException
 */
@RequestMapping("get-captcha")
public ResponseEntity<byte[]> getCaptcha(JpegImgCaptchaToken token, HttpSession session) throws IOException {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.IMAGE_JPEG);

    captchaManager.setCurrentSession(session);
    Captcha captcha = captchaManager.create(token);

    return new ResponseEntity<>(captcha.getStream(), headers, HttpStatus.OK);
}

From source file:org.keycloak.adapters.springsecurity.service.KeycloakDirectAccessGrantService.java

@Override
public RefreshableKeycloakSecurityContext login(String username, String password) throws VerificationException {

    final MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
    final HttpHeaders headers = new HttpHeaders();

    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    body.set("username", username);
    body.set("password", password);
    body.set(OAuth2Constants.GRANT_TYPE, OAuth2Constants.PASSWORD);

    AccessTokenResponse response = template.postForObject(keycloakDeployment.getTokenUrl(),
            new HttpEntity<>(body, headers), AccessTokenResponse.class);

    return KeycloakSpringAdapterUtils.createKeycloakSecurityContext(keycloakDeployment, response);
}

From source file:com.github.dactiv.fear.service.web.SystemCommonController.java

/**
 * ? freemarker ?/*from   ww  w .  ja  v a 2s  . c  o  m*/
 *
 * @param name ???
 *
 * @return ?
 */
@RequestMapping("get-ftl-template")
public ResponseEntity<byte[]> getFtlTemplate(@RequestParam String name, HttpServletRequest request)
        throws IOException {

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.TEXT_PLAIN);

    String path = request.getSession().getServletContext().getRealPath("") + ftlTemplatePath + name + ".ftl";
    byte[] bytes = FileUtils.readFileToByteArray(new File(path));

    return new ResponseEntity<>(bytes, headers, HttpStatus.OK);
}

From source file:edu.fing.tagsi.db4o.business.TrackingController.java

public List<Tracking> getTracking(UUID id) {
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity entity = new HttpEntity(headers);

    ResponseEntity<RequestTrackingAddPackage[]> tracking = restTemplate.exchange(
            ConfigController.getInstance().getURLFindTracking() + "/" + id.toString(), GET, entity,
            RequestTrackingAddPackage[].class);

    if (tracking != null) {
        List<Tracking> resultado = new ArrayList<>(tracking.getBody().length);
        for (RequestTrackingAddPackage r : tracking.getBody()) {

            resultado.add(new Tracking(UUID.fromString(r.getIdpaquete()), UUID.fromString(r.getIdcliente()),
                    UUID.fromString(r.getIdlugar()), r.getFecha(), r.isEsdestino()));
        }/*  w w w.j  ava  2s.  c om*/
        return resultado;
    } else {
        return null;
    }

}

From source file:org.openbaton.nfvo.api.exceptions.GlobalExceptionHandler.java

@ExceptionHandler({ NotFoundException.class, NoResultException.class })
@ResponseStatus(value = HttpStatus.NOT_FOUND)
protected ResponseEntity<Object> handleNotFoundException(Exception e, WebRequest request) {
    if (log.isDebugEnabled()) {
        log.error("Exception was thrown -> Return message: " + e.getMessage(), e);
    } else {//from  ww  w. j  a v a 2 s . c  o m
        log.error("Exception was thrown -> Return message: " + e.getMessage());
    }
    ExceptionResource exc = new ExceptionResource("Not Found", e.getMessage());
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);

    return handleExceptionInternal(e, exc, headers, HttpStatus.UNPROCESSABLE_ENTITY, request);
}

From source file:org.openbaton.nfvo.api.exceptions.GlobalExceptionHandler.java

@ExceptionHandler({ UnauthorizedUserException.class })
@ResponseStatus(value = HttpStatus.FORBIDDEN)
protected ResponseEntity<Object> handleUnauthorized(Exception e, WebRequest request) {
    if (log.isDebugEnabled()) {
        log.error("Exception was thrown -> Return message: " + e.getMessage(), e);
    } else {/*from   w w w .  j a v  a  2s  . c o  m*/
        log.error("Exception was thrown -> Return message: " + e.getMessage());
    }
    ExceptionResource exc = new ExceptionResource("Unauthorized exception", e.getMessage());
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);

    return handleExceptionInternal(e, exc, headers, HttpStatus.FORBIDDEN, request);
}

From source file:edu.fing.tagsi.db4o.business.CiudadesController.java

public Recorrido ObtenerRecorrido(Ciudad origen, Ciudad destino) {
    RestTemplate restTemplate = new RestTemplate();
    RequestRecorrido req = new RequestRecorrido(origen.getNombre(), destino.getNombre());
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<RequestRecorrido> entity = new HttpEntity<>(req, headers);
    RecorridoREST recorrido = restTemplate.postForObject(URL, entity, RecorridoREST.class);

    if (recorrido.getCiudades() != null) {
        NodoCamino[] ciudades = new NodoCamino[recorrido.getCiudades().length];
        for (int i = 0; i < ciudades.length; i++) {
            if (i == 0) {
                ciudades[i] = new NodoCamino(ObtenerCiudad(recorrido.getCiudades()[i].getName()), true, 0,
                        null);/*  w  w w .j a v  a2 s .c om*/
            } else {
                Tramo tramo = recorrido.getRutas()[i - 1];
                ciudades[i] = new NodoCamino(ObtenerCiudad(recorrido.getCiudades()[i].getName()), false,
                        tramo.getDistancia(), tramo.getRutas());
            }
        }
        Recorrido resultado = new Recorrido(recorrido.getDistanciaTotal(), ciudades);
        return resultado;
    } else {
        return null;
    }

}

From source file:org.makersoft.mvc.unit.FormatHandlerMethodReturnValueHandlerTests.java

@Test
public void testExcludes() throws Exception {
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.APPLICATION_JSON);
    mockMvc.perform(get("/account/user/excludes/1").accept(MediaType.APPLICATION_JSON).headers(httpHeaders))
            .andDo(print()).andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON)).andExpect(content().encoding("UTF-8"))
            .andExpect(jsonPath("$.id").value(1)).andExpect(jsonPath("$.password").doesNotExist())
            .andExpect(jsonPath("$.dept").doesNotExist()).andExpect(jsonPath("$.roles").doesNotExist());

}

From source file:org.makersoft.mvc.unit.FormatHandlerMethodReturnValueHandlerTests.java

@Test
public void testExcludeEntityAttributes() throws Exception {
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.APPLICATION_JSON);
    mockMvc.perform(get("/account/user//exclude-entity-attributes/1").accept(MediaType.APPLICATION_JSON)
            .headers(httpHeaders)).andDo(print()).andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON)).andExpect(content().encoding("UTF-8"))
            .andExpect(jsonPath("$.id").value(1)).andExpect(jsonPath("$.dept.users").doesNotExist())
            .andExpect(jsonPath("$.roles.users").doesNotExist());

}