Example usage for org.springframework.web.util UriComponentsBuilder fromUri

List of usage examples for org.springframework.web.util UriComponentsBuilder fromUri

Introduction

In this page you can find the example usage for org.springframework.web.util UriComponentsBuilder fromUri.

Prototype

public static UriComponentsBuilder fromUri(URI uri) 

Source Link

Document

Create a builder that is initialized with the given URI .

Usage

From source file:software.coolstuff.springframework.owncloud.service.AbstractOwncloudResourceServiceTest.java

private URI appendPath(URI baseUri, String appendPath) {
    if (baseUri == null) {
        return Optional.ofNullable(appendPath).map(path -> URI.create(path).normalize()).orElse(baseUri);
    }/*from   ww  w.  j  av a2 s .co m*/
    return URI.create(UriComponentsBuilder.fromUri(baseUri).path(appendPath).toUriString()).normalize();
}

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   ww  w. j ava  2 s  . c o 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.rest.OwncloudRestResourceServiceTest.java

private URI resolveAsFileURI(URI relativeTo) {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    return URI.create(UriComponentsBuilder.fromUri(getResolvedRootUri(authentication.getName()))
            .path(relativeTo.getPath()).toUriString());
}

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

private URI resolveAsDirectoryURI(URI relativeTo, String username) {
    URI resolvedRootUri = getResolvedRootUri(username);
    if (relativeTo == null || StringUtils.isBlank(relativeTo.getPath())) {
        return resolvedRootUri;
    }/*from  ww  w  .  j av a2s.c  o m*/
    return URI.create(
            UriComponentsBuilder.fromUri(resolvedRootUri).path(relativeTo.getPath()).path("/").toUriString());
}