Example usage for org.springframework.http ResponseEntity ok

List of usage examples for org.springframework.http ResponseEntity ok

Introduction

In this page you can find the example usage for org.springframework.http ResponseEntity ok.

Prototype

public static BodyBuilder ok() 

Source Link

Document

Create a builder with the status set to HttpStatus#OK OK .

Usage

From source file:org.apache.servicecomb.demo.springmvc.server.DownloadSchema.java

@GetMapping(path = "/tempFileEntity")
public ResponseEntity<Part> tempFileEntity(String content) throws IOException {
    File file = createTempFile(content);

    return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=tempFileEntity.txt")
            .body(new FilePart(null, file).setDeleteAfterFinished(true));
}

From source file:org.apache.servicecomb.demo.springmvc.server.DownloadSchema.java

@GetMapping(path = "/entityResource")
@ApiResponses({ @ApiResponse(code = 200, response = File.class, message = ""), })
public ResponseEntity<Resource> entityResource(String content) throws IOException {
    return ResponseEntity.ok().header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE)
            .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=entityResource.txt")
            .body(new ByteArrayResource(content.getBytes(StandardCharsets.UTF_8)));
}

From source file:org.apache.servicecomb.demo.springmvc.server.DownloadSchema.java

@GetMapping(path = "/entityInputStream")
@ApiResponses({ @ApiResponse(code = 200, response = File.class, message = ""), })
public ResponseEntity<InputStream> entityInputStream(String content) throws IOException {
    return ResponseEntity.ok().header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE)
            .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=entityInputStream.txt")
            .body(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)));
}

From source file:org.apache.servicecomb.demo.springmvc.server.DownloadSchema.java

@GetMapping(path = "/bytes")
@ApiResponses({ @ApiResponse(code = 200, response = File.class, message = ""), })
public ResponseEntity<byte[]> bytes(String content) throws IOException {
    return ResponseEntity.ok().header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE)
            .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=bytes.txt")
            .body(content.getBytes(StandardCharsets.UTF_8));
}

From source file:org.apache.servicecomb.demo.springmvc.server.DownloadSchema.java

@GetMapping(path = "/netInputStream")
@ApiResponses({ @ApiResponse(code = 200, response = File.class, message = ""), })
public ResponseEntity<InputStream> netInputStream(String content) throws IOException {
    URL url = new URL("http://localhost:9000/download/netInputStream?content="
            + URLEncoder.encode(content, StandardCharsets.UTF_8.name()));
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    return ResponseEntity.ok().header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE)
            .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=netInputStream.txt")
            .body(conn.getInputStream());
}

From source file:org.apache.servicecomb.it.schema.DownloadSchema.java

@GetMapping(path = "/entityResource")
@ApiResponses({ @ApiResponse(code = 200, response = File.class, message = ""), })
public ResponseEntity<Resource> entityResource(String content) {
    return ResponseEntity.ok().header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE)
            .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=entityResource.txt")
            .body(new ByteArrayResource(content.getBytes(StandardCharsets.UTF_8)));
}

From source file:org.apache.servicecomb.it.schema.DownloadSchema.java

@GetMapping(path = "/entityInputStream")
@ApiResponses({ @ApiResponse(code = 200, response = File.class, message = ""), })
public ResponseEntity<InputStream> entityInputStream(String content) {
    return ResponseEntity.ok().header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE)
            .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=entityInputStream.txt")
            .body(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)));
}

From source file:org.apache.servicecomb.it.schema.DownloadSchema.java

@GetMapping(path = "/bytes")
@ApiResponses({ @ApiResponse(code = 200, response = File.class, message = ""), })
public ResponseEntity<byte[]> bytes(String content) {
    return ResponseEntity.ok().header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE)
            .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=bytes.txt")
            .body(content.getBytes(StandardCharsets.UTF_8));
}

From source file:org.apache.servicecomb.it.schema.DownloadSchema.java

@GetMapping(path = "/netInputStream")
@ApiResponses({ @ApiResponse(code = 200, response = File.class, message = ""), })
public ResponseEntity<InputStream> netInputStream(String content) throws IOException {
    URL url = new URL("http://localhost:" + server.getLocalPort() + "/download/netInputStream?content="
            + URLEncoder.encode(content, StandardCharsets.UTF_8.name()));
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    ResponseEntity<InputStream> responseEntity = ResponseEntity.ok()
            .header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE)
            .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=netInputStream.txt")
            .body(conn.getInputStream());
    conn.disconnect();//from www . j  ava  2  s  . co m
    return responseEntity;
}

From source file:org.eclipse.hawkbit.mgmt.rest.resource.MgmtDownloadResource.java

/**
 * Handles the GET request for downloading an artifact.
 * /*from  w ww .  j a  v a 2  s.c  om*/
 * @param downloadId
 *            the generated download id
 * @return {@link ResponseEntity} with status {@link HttpStatus#OK} if
 *         successful
 */
@Override
@ResponseBody
public ResponseEntity<Void> downloadArtifactByDownloadId(@PathVariable("downloadId") final String downloadId) {
    try {
        final ValueWrapper cacheWrapper = cache.get(downloadId);
        if (cacheWrapper == null) {
            LOGGER.warn("Download Id {} could not be found", downloadId);
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }

        final DownloadArtifactCache artifactCache = (DownloadArtifactCache) cacheWrapper.get();
        DbArtifact artifact = null;

        if (DownloadType.BY_SHA1.equals(artifactCache.getDownloadType())) {
            artifact = artifactRepository.getArtifactBySha1(artifactCache.getId());
        } else {
            LOGGER.warn("Download Type {} not supported", artifactCache.getDownloadType());
        }

        if (artifact == null) {
            LOGGER.warn("Artifact with cached id {} and download type {} could not be found.",
                    artifactCache.getId(), artifactCache.getDownloadType());
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        try {
            IOUtils.copy(artifact.getFileInputStream(),
                    requestResponseContextHolder.getHttpServletResponse().getOutputStream());
        } catch (final IOException e) {
            LOGGER.error("Cannot copy streams", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    } finally {
        cache.evict(downloadId);
    }

    return ResponseEntity.ok().build();
}