Example usage for org.springframework.http MediaType valueOf

List of usage examples for org.springframework.http MediaType valueOf

Introduction

In this page you can find the example usage for org.springframework.http MediaType valueOf.

Prototype

public static MediaType valueOf(String value) 

Source Link

Document

Parse the given String value into a MediaType object, with this method name following the 'valueOf' naming convention (as supported by org.springframework.core.convert.ConversionService .

Usage

From source file:org.springframework.xd.dirt.plugins.stream.StreamPlugin.java

private Collection<MediaType> getAcceptedMediaTypes(Module module) {
    Collection<?> acceptedTypes = module.getComponent(CONTENT_TYPE_BEAN_NAME, Collection.class);

    if (CollectionUtils.isEmpty(acceptedTypes)) {
        return DEFAULT_ACCEPTED_CONTENT_TYPES;
    } else {/*from w  w w  . j  ava  2  s  . c o m*/
        Collection<MediaType> acceptedMediaTypes = new ArrayList<MediaType>(acceptedTypes.size());
        for (Object acceptedType : acceptedTypes) {
            if (acceptedType instanceof String) {
                acceptedMediaTypes.add(MediaType.valueOf((String) acceptedType));
            } else if (acceptedType instanceof MediaType) {
                acceptedMediaTypes.add((MediaType) acceptedType);
            } else {
                throw new IllegalArgumentException("Unrecognized MediaType :" + acceptedType);
            }
        }
        return Collections.unmodifiableCollection(acceptedMediaTypes);
    }
}

From source file:ren.hankai.cordwood.web.security.support.DefaultRequestInspector.java

@Override
public boolean verifyRequestParameters(HttpServletRequest request, String sk) {
    final Map<String, String[]> params = request.getParameterMap();
    // ? form//ww w  .  j a  va 2  s.  c  om
    final MediaType contentType = MediaType.valueOf(request.getContentType());
    if (MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(contentType)) {
        // URL??
        return verifyRequestParameters(params, sk);
    } else if (MediaType.APPLICATION_JSON.isCompatibleWith(contentType)
            || MediaType.APPLICATION_XML.isCompatibleWith(contentType)) {
        // ?URL??
        String requestBody = null;
        try {
            requestBody = IOUtils.toString(request.getInputStream());
        } catch (final IOException ex) {
            logger.warn("Failed to verify request parameters due to io error while parsing request body.", ex);
            return false;
        }
        final String expSign = signRequestBody(requestBody, sk);
        final Object sign = params.get(RequestInspector.REQUEST_SIGN);
        if (sign != null) {
            if ((sign instanceof String) && expSign.equalsIgnoreCase((String) sign)) {
                return true;
            } else if (sign instanceof String[]) {
                final String[] strArr = (String[]) sign;
                if ((strArr.length > 0) && expSign.equalsIgnoreCase(strArr[0])) {
                    return true;
                }
            }
        }
        return false;
    } else if (MediaType.MULTIPART_FORM_DATA.isCompatibleWith(contentType)) {
        // TODO: ??
        return true;
    }
    return false;
}

From source file:software.coolstuff.springframework.owncloud.service.impl.local.OwncloudLocalResourceServiceImpl.java

private OwncloudLocalResourceExtension createOwncloudResourceOf(Path path) {
    Path rootPath = getRootLocationOfAuthenticatedUser();
    Path relativePath = rootPath.toAbsolutePath().relativize(path.toAbsolutePath());
    URI href = URI.create(UriComponentsBuilder.fromPath("/").path(relativePath.toString()).toUriString());
    String name = path.getFileName().toString();
    MediaType mediaType = MediaType.APPLICATION_OCTET_STREAM;
    if (Files.isDirectory(path)) {
        href = URI.create(UriComponentsBuilder.fromUri(href).path("/").toUriString());
        mediaType = OwncloudUtils.getDirectoryMediaType();
    } else {/*from   w ww .j a v  a2  s  . co  m*/
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        String contentType = fileNameMap.getContentTypeFor(path.getFileName().toString());
        if (StringUtils.isNotBlank(contentType)) {
            mediaType = MediaType.valueOf(contentType);
        }
    }
    try {
        LocalDateTime lastModifiedAt = LocalDateTime.ofInstant(Files.getLastModifiedTime(path).toInstant(),
                ZoneId.systemDefault());
        Optional<String> checksum = checksumService.getChecksum(path);
        if (Files.isSameFile(rootPath, path)) {
            name = "/";
            checksum = Optional.empty();
        }
        OwncloudLocalResourceExtension resource = OwncloudLocalResourceImpl.builder().href(href).name(name)
                .eTag(checksum.orElse(null)).mediaType(mediaType).lastModifiedAt(lastModifiedAt).build();
        if (Files.isDirectory(path)) {
            return resource;
        }

        return OwncloudLocalFileResourceImpl.fileBuilder().owncloudResource(resource)
                .contentLength(Files.size(path)).build();
    } catch (NoSuchFileException e) {
        throw new OwncloudResourceNotFoundException(href, getUsername());
    } catch (IOException e) {
        val logMessage = String.format("Cannot create OwncloudResource from Path %s", path);
        log.error(logMessage, e);
        throw new OwncloudLocalResourceException(logMessage, e);
    }
}

From source file:software.coolstuff.springframework.owncloud.service.impl.OwncloudUtils.java

/**
 * Get the MediaType of a Directory (<code>httpd/unix-directory</code>)
 * @return MediaType of a Directory/*from w  w  w  .j  ava 2 s.co  m*/
 */
public static MediaType getDirectoryMediaType() {
    return MediaType.valueOf(UNIX_DIRECTORY);
}

From source file:software.coolstuff.springframework.owncloud.service.impl.rest.OwncloudRestResourceServiceImpl.java

private OwncloudRestResourceExtension createOwncloudResourceFrom(DavResource davResource,
        OwncloudResourceConversionProperties conversionProperties) {
    log.debug("Create OwncloudResource based on DavResource {}", davResource.getHref());
    MediaType mediaType = MediaType.valueOf(davResource.getContentType());
    URI rootPath = conversionProperties.getRootPath();
    URI href = rootPath.resolve(davResource.getHref());
    String name = davResource.getName();
    if (davResource.isDirectory() && href.equals(rootPath)) {
        name = SLASH;//from ww w  .j a  v  a  2 s .c om
    }
    LocalDateTime lastModifiedAt = LocalDateTime.ofInstant(davResource.getModified().toInstant(),
            ZoneId.systemDefault());
    href = rootPath.relativize(href);
    href = URI.create(SLASH).resolve(href).normalize(); // prepend "/" to the href
    OwncloudRestResourceExtension owncloudResource = OwncloudRestResourceImpl.builder().href(href).name(name)
            .lastModifiedAt(lastModifiedAt).mediaType(mediaType)
            .eTag(StringUtils.strip(davResource.getEtag(), QUOTE)).build();
    if (davResource.isDirectory()) {
        return owncloudResource;
    }
    return OwncloudRestFileResourceImpl.fileBuilder().owncloudResource(owncloudResource)
            .contentLength(davResource.getContentLength()).build();
}