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:org.projecthdata.social.api.impl.HDataTemplateTest.java

@Test
public void test() {
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.setContentType(MediaType.APPLICATION_XML);

    mockServer.expect(requestTo("http://hstore.com/1234/root.xml")).andExpect(method(GET)).andRespond(
            withResponse(new ClassPathResource("../../resources/root.xml", getClass()), responseHeaders));

    Root root = hDataTemplate.getRootOperations().getRoot();
    assertEquals(5, root.getExtensions().size());
}

From source file:io.github.microcks.web.ExportController.java

@RequestMapping(value = "/export", method = RequestMethod.GET)
public ResponseEntity<?> exportRepository(@RequestParam(value = "serviceIds") List<String> serviceIds) {
    log.debug("Extracting export for serviceIds {}", serviceIds);
    String json = importExportService.exportRepository(serviceIds, "json");

    byte[] body = json.getBytes();
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.setContentType(MediaType.APPLICATION_JSON);
    responseHeaders.set("Content-Disposition", "attachment; filename=microcks-repository.json");
    responseHeaders.setContentLength(body.length);

    return new ResponseEntity<Object>(body, responseHeaders, HttpStatus.OK);
}

From source file:com.castlemock.web.basis.web.mvc.controller.project.ExportProjectController.java

/**
 * Creates and return a view that provides the required functionality
 * to export a project./*from w  ww . j a v a 2s  .c  o  m*/
 * @param projectType The type of the project that should be exported
 * @param projectId The id of the project that should be exported
 * @return A view that provides the required functionality to export a project
 */
@PreAuthorize("hasAuthority('READER') or hasAuthority('MODIFIER') or hasAuthority('ADMIN')")
@RequestMapping(value = "{projectType}/project/{projectId}/export", method = RequestMethod.GET)
public ResponseEntity<String> defaultPage(@PathVariable final String projectType,
        @PathVariable final String projectId) {
    final String exportedProject = projectServiceFacade.exportProject(projectType, projectId);

    HttpHeaders respHeaders = new HttpHeaders();
    respHeaders.setContentType(MediaType.TEXT_XML);
    respHeaders.setContentDispositionFormData("attachment",
            "project-" + projectType + "-" + projectId + ".xml");

    return new ResponseEntity<String>(exportedProject, respHeaders, HttpStatus.OK);
}

From source file:fi.helsinki.opintoni.web.rest.publicapi.PublicImageResource.java

private HttpHeaders headersWithContentType(MediaType contentType) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(contentType);
    return headers;
}

From source file:org.openschedule.api.impl.SessionTemplate.java

public void addBlockComment(String shortName, Integer dayId, Integer scheduleId, Integer blockId,
        Comment comment) {//from   w ww  . jav  a2  s  .  com
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.setContentType(new MediaType("application", "json"));

    HttpEntity<Comment> requestEntity = new HttpEntity<Comment>(comment, requestHeaders);

    restTemplate.exchange(
            "public/" + shortName + "/days/" + dayId + "/schedules/" + scheduleId + "/blocks/" + blockId
                    + "/comments",
            HttpMethod.POST, requestEntity, String.class, shortName, dayId, scheduleId, blockId).getBody();
}

From source file:com.fabionoth.rest.RobotRest.java

/**
 *
 * @param command/*from w w  w . j a  v a 2s .co  m*/
 * @return
 */
@RequestMapping(value = { "rest/mars/", "/rest/mars/{command}" }, method = { RequestMethod.GET,
        RequestMethod.POST })
public ResponseEntity<String> sendCommand(@PathVariable Optional<String> command) {
    Robot robot;
    robot = new Robot(new Long(1), 0, 0, CardinalPoints.N);

    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.setContentType(MediaType.TEXT_HTML);

    if (command.isPresent()) {
        try {
            robot = new RobotController(robot, command.get()).getRobot();
        } catch (Exception ex) {
            Logger.getLogger(RobotRest.class.getName()).log(Level.SEVERE, null, ex);
            return new ResponseEntity<>("Erro", responseHeaders, HttpStatus.BAD_REQUEST);
        }
    }

    String response = "(" + robot.getX() + ", " + robot.getY() + ", " + robot.getC().toString() + ")";
    return new ResponseEntity<>(response, responseHeaders, HttpStatus.OK);

}

From source file:com.xyxy.platform.examples.showcase.demos.hystrix.web.HystrixExceptionHandler.java

/**
 * ?Hystrix Runtime, :/* w w  w. j  a v a 2  s. co  m*/
 * Command(500).
 * Hystrix??(503).
 */
@ExceptionHandler(value = { HystrixRuntimeException.class })
public final ResponseEntity<?> handleException(HystrixRuntimeException e, WebRequest request) {
    HttpStatus status = HttpStatus.SERVICE_UNAVAILABLE;
    String message = e.getMessage();

    FailureType type = e.getFailureType();

    // ?
    if (type.equals(FailureType.COMMAND_EXCEPTION)) {
        status = HttpStatus.INTERNAL_SERVER_ERROR;
        message = Exceptions.getErrorMessageWithNestedException(e);
    }

    logger.error(message, e);

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.parseMediaType(MediaTypes.TEXT_PLAIN_UTF_8));
    return handleExceptionInternal(e, message, headers, status, request);
}

From source file:com.aspose.showcase.qrcodegen.web.api.controller.RestResponseEntityExceptionHandler.java

@ExceptionHandler(value = { BarCodeException.class })
protected ResponseEntity<Object> handleConflict(RuntimeException ex, WebRequest request) {

    String bodyOfResponse = "Internal Server Error";
    HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;

    String detailMessage = ex.getLocalizedMessage();

    if (detailMessage == null) {
        bodyOfResponse = "Internal Server Error";
        httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;

    } else if (detailMessage.contains("evaluation version")) {

        bodyOfResponse = "Please upgrade to paid license to avail this feature. \n Internal Error - "
                + ex.getMessage();/* w  w w  .  j ava2  s  . co m*/
        httpStatus = HttpStatus.PAYMENT_REQUIRED;

    } else {
        bodyOfResponse = ex.getMessage();
        httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;

    }

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

    return handleExceptionInternal(ex, bodyOfResponse, headers, httpStatus, request);
}

From source file:org.awesomeagile.integrations.hackpad.RestTemplateHackpadClient.java

@Override
public PadIdentity createHackpad(String title) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.TEXT_PLAIN);
    HttpEntity<String> entity = new HttpEntity<>(title, headers);
    return restTemplate.postForObject(fullUrl(CREATE_URL), entity, PadIdentity.class);
}

From source file:org.awesomeagile.webapp.security.google.GoogleConfigurableOAuth2Template.java

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
protected AccessGrant postForAccessGrant(String accessTokenUrl, MultiValueMap<String, String> parameters) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(
            parameters, headers);//from  w  w w.  jav a 2 s.  c  o m
    ResponseEntity<Map> responseEntity = getRestTemplate().exchange(accessTokenUrl, HttpMethod.POST,
            requestEntity, Map.class);
    Map<String, Object> responseMap = responseEntity.getBody();
    return extractAccessGrant(responseMap);
}