Example usage for org.springframework.http HttpHeaders setConnection

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

Introduction

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

Prototype

public void setConnection(List<String> connection) 

Source Link

Document

Set the (new) value of the Connection header.

Usage

From source file:org.messic.server.facade.controllers.rest.SongController.java

@ApiMethod(path = "/services/songs/{songSid}/dlna", verb = ApiVerb.GET, description = "Get the audio binary from a song resource of an album for a dlna service", produces = {
        MediaType.APPLICATION_OCTET_STREAM_VALUE })
@ApiErrors(apierrors = { @ApiError(code = UnknownMessicRESTException.VALUE, description = "Unknown error"),
        @ApiError(code = NotAuthorizedMessicRESTException.VALUE, description = "Forbidden access"),
        @ApiError(code = IOMessicRESTException.VALUE, description = "IO error trying to get the audio resource") })
@RequestMapping(value = "/{songSid}/dlna", method = { RequestMethod.GET, RequestMethod.HEAD })
@ResponseStatus(HttpStatus.OK)//  w w w .  j a  v a 2s.  com
@ResponseBody
@ApiResponseObject
public ResponseEntity getSongDLNA(
        @ApiParam(name = "songSid", description = "SID of the song resource we want to download", paramType = ApiParamType.PATH, required = true) @PathVariable Long songSid,
        HttpServletRequest request, HttpServletResponse response)
        throws NotAuthorizedMessicRESTException, IOMessicRESTException, UnknownMessicRESTException {
    User user = SecurityUtil.getCurrentUser();

    try {

        HttpHeaders headers = new HttpHeaders();

        // TODO some mp3 songs fail with application/octet-stream
        // MP3 files must have the content type of audio/mpeg or application/octet-stream
        // ogg files must have the content type of application/ogg

        headers.setContentType(MediaType.parseMediaType("audio/mpeg"));

        headers.setConnection("close");
        headers.add("EXT", null);
        headers.add("Accept-Ranges", "bytes");
        headers.add("transferMode.dlna.org", "Streaming");
        headers.add("contentFeatures.dlna.org", "DLNA.ORG_PN=MP3;DLNA.ORG_OP=01;DLNA.ORG_CI=0");
        headers.add("realTimeInfo.dlna.org", "DLNA.ORG_TLAG=*");
        headers.setDate(System.currentTimeMillis());

        if (request.getHeader("Range") == null) {
            APISong.AudioSongStream ass = songAPI.getAudioSong(user, songSid);
            headers.setContentLength(ass.contentLength);

            headers.add("Content-Range", "bytes 0-" + ass.contentLength + "/" + ass.contentLength);

            InputStreamResource inputStreamResource = new InputStreamResource(ass.is);

            if (request.getMethod().equalsIgnoreCase("GET")) {
                return new ResponseEntity(inputStreamResource, headers, HttpStatus.OK);
            } else {
                return new ResponseEntity<byte[]>(new byte[0], headers, HttpStatus.OK);
            }
        } else {
            APISong.AudioSongStream ass = songAPI.getAudioSong(user, songSid);

            String range = request.getHeader("Range");
            String[] sbytes = range.split("=")[1].split("-");
            int from = Integer.valueOf(sbytes[0]);
            int to = (int) ass.contentLength;
            if (sbytes.length > 1) {
                to = Integer.valueOf(sbytes[1]);
            }

            headers.setContentLength(to - from);
            headers.add("Content-Range", "bytes " + from + "-" + to + "/" + ass.contentLength);

            if (request.getMethod().equalsIgnoreCase("GET")) {
                UtilSubInputStream usi = new UtilSubInputStream(ass.is, from, to);
                InputStreamResource inputStreamResource = new InputStreamResource(usi);
                return new ResponseEntity(inputStreamResource, headers, HttpStatus.OK);
            } else {
                return new ResponseEntity<byte[]>(new byte[0], headers, HttpStatus.OK);
            }
        }
    } catch (IOException ioe) {
        log.error("failed!", ioe);
        throw new IOMessicRESTException(ioe);
    } catch (Exception e) {
        throw new UnknownMessicRESTException(e);
    }
}

From source file:net.paslavsky.springrest.HttpHeadersHelper.java

public HttpHeaders getHttpHeaders(Map<String, Integer> headerParameters, Object[] arguments) {
    HttpHeaders headers = new HttpHeaders();
    for (String headerName : headerParameters.keySet()) {
        Object headerValue = arguments[headerParameters.get(headerName)];
        if (headerValue != null) {
            if (ACCEPT.equalsIgnoreCase(headerName)) {
                headers.setAccept(toList(headerValue, MediaType.class));
            } else if (ACCEPT_CHARSET.equalsIgnoreCase(headerName)) {
                headers.setAcceptCharset(toList(headerValue, Charset.class));
            } else if (ALLOW.equalsIgnoreCase(headerName)) {
                headers.setAllow(toSet(headerValue, HttpMethod.class));
            } else if (CONNECTION.equalsIgnoreCase(headerName)) {
                headers.setConnection(toList(headerValue, String.class));
            } else if (CONTENT_DISPOSITION.equalsIgnoreCase(headerName)) {
                setContentDisposition(headers, headerName, headerValue);
            } else if (CONTENT_LENGTH.equalsIgnoreCase(headerName)) {
                headers.setContentLength(toLong(headerValue));
            } else if (CONTENT_TYPE.equalsIgnoreCase(headerName)) {
                headers.setContentType(toMediaType(headerValue));
            } else if (DATE.equalsIgnoreCase(headerName)) {
                headers.setDate(toLong(headerValue));
            } else if (ETAG.equalsIgnoreCase(headerName)) {
                headers.setETag(toString(headerValue));
            } else if (EXPIRES.equalsIgnoreCase(headerName)) {
                headers.setExpires(toLong(headerValue));
            } else if (IF_MODIFIED_SINCE.equalsIgnoreCase(headerName)) {
                headers.setIfModifiedSince(toLong(headerValue));
            } else if (IF_NONE_MATCH.equalsIgnoreCase(headerName)) {
                headers.setIfNoneMatch(toList(headerValue, String.class));
            } else if (LAST_MODIFIED.equalsIgnoreCase(headerName)) {
                headers.setLastModified(toLong(headerValue));
            } else if (LOCATION.equalsIgnoreCase(headerName)) {
                headers.setLocation(toURI(headerValue));
            } else if (ORIGIN.equalsIgnoreCase(headerName)) {
                headers.setOrigin(toString(headerValue));
            } else if (PRAGMA.equalsIgnoreCase(headerName)) {
                headers.setPragma(toString(headerValue));
            } else if (UPGRADE.equalsIgnoreCase(headerName)) {
                headers.setUpgrade(toString(headerValue));
            } else if (headerValue instanceof String) {
                headers.set(headerName, (String) headerValue);
            } else if (headerValue instanceof String[]) {
                headers.put(headerName, Arrays.asList((String[]) headerValue));
            } else if (instanceOf(headerValue, String.class)) {
                headers.put(headerName, toList(headerValue, String.class));
            } else {
                headers.set(headerName, conversionService.convert(headerValue, String.class));
            }//from  ww  w  .j a va  2s  .  co  m
        }
    }
    return headers;
}