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

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

Introduction

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

Prototype

public static UriComponentsBuilder newInstance() 

Source Link

Document

Create a new, empty builder.

Usage

From source file:org.cloudfoundry.identity.client.UaaContextFactory.java

/**
 * Returns the authorize URI//from   www  .j  a v a2  s  .c om
 * @return the UAA authorization URI
 */
public URI getAuthorizeUri() {
    UriComponentsBuilder authorizationURI = UriComponentsBuilder.newInstance();
    authorizationURI.uri(uaaURI);
    authorizationURI.path(authorizePath);
    return authorizationURI.build().toUri();
}

From source file:org.cloudfoundry.identity.client.UaaContextFactory.java

/**
 * Returns the URI// w w  w .j a v a 2 s . c om
 * @return
 */
public URI getTokenUri() {
    UriComponentsBuilder tokenURI = UriComponentsBuilder.newInstance();
    tokenURI.uri(uaaURI);
    tokenURI.path(tokenPath);
    return tokenURI.build().toUri();
}

From source file:org.joyrest.oauth2.endpoint.AuthorizationEndpoint.java

private String append(String base, Map<String, ?> query, Map<String, String> keys, boolean fragment) {

    UriComponentsBuilder template = UriComponentsBuilder.newInstance();
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(base);
    URI redirectUri;/* www  . j  a  v  a2  s.c o m*/
    try {
        // assume it's encoded to start with (if it came in over the wire)
        redirectUri = builder.build(true).toUri();
    } catch (Exception e) {
        // ... but allow client registrations to contain hard-coded non-encoded values
        redirectUri = builder.build().toUri();
        builder = UriComponentsBuilder.fromUri(redirectUri);
    }
    template.scheme(redirectUri.getScheme()).port(redirectUri.getPort()).host(redirectUri.getHost())
            .userInfo(redirectUri.getUserInfo()).path(redirectUri.getPath());

    if (fragment) {
        StringBuilder values = new StringBuilder();
        if (redirectUri.getFragment() != null) {
            String append = redirectUri.getFragment();
            values.append(append);
        }
        for (String key : query.keySet()) {
            if (values.length() > 0) {
                values.append("&");
            }
            String name = key;
            if (keys != null && keys.containsKey(key)) {
                name = keys.get(key);
            }
            values.append(name + "={" + key + "}");
        }
        if (values.length() > 0) {
            template.fragment(values.toString());
        }
        UriComponents encoded = template.build().expand(query).encode();
        builder.fragment(encoded.getFragment());
    } else {
        for (String key : query.keySet()) {
            String name = key;
            if (nonNull(keys) && keys.containsKey(key)) {
                name = keys.get(key);
            }
            template.queryParam(name, "{" + key + "}");
        }
        template.fragment(redirectUri.getFragment());
        UriComponents encoded = template.build().expand(query).encode();
        builder.query(encoded.getQuery());
    }

    return builder.build().toUriString();
}

From source file:org.haiku.haikudepotserver.job.controller.JobController.java

/**
 * <p>This is helper-code that can be used to check to see if the data is stale and
 * will then enqueue the job, run it and then redirect the user to the data
 * download.</p>/*from  w w w . ja  v a2s  .  c  o  m*/
 * @param response is the HTTP response to send the redirect to.
 * @param ifModifiedSinceHeader is the inbound header from the client.
 * @param lastModifyTimestamp is the actual last modified date for the data.
 * @param jobSpecification is the job that would be run if the data is newer than in the
 *                         inbound header.
 */

public static void handleRedirectToJobData(HttpServletResponse response, JobService jobService,
        String ifModifiedSinceHeader, Date lastModifyTimestamp, JobSpecification jobSpecification)
        throws IOException {

    if (!Strings.isNullOrEmpty(ifModifiedSinceHeader)) {
        try {
            Date requestModifyTimestamp = new Date(Instant
                    .from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(ifModifiedSinceHeader)).toEpochMilli());

            if (requestModifyTimestamp.getTime() >= lastModifyTimestamp.getTime()) {
                response.setStatus(HttpStatus.NOT_MODIFIED.value());
                return;
            }
        } catch (DateTimeParseException dtpe) {
            LOGGER.warn("bad [{}] header on request; [{}] -- will ignore", HttpHeaders.IF_MODIFIED_SINCE,
                    StringUtils.abbreviate(ifModifiedSinceHeader, 128));
        }
    }

    // what happens here is that we get the report and if it is too old, delete it and try again.

    JobSnapshot jobSnapshot = getJobSnapshotStartedAfter(jobService, lastModifyTimestamp, jobSpecification);
    Set<String> jobDataGuids = jobSnapshot.getDataGuids();

    if (1 != jobDataGuids.size()) {
        throw new IllegalStateException("found [" + jobDataGuids.size()
                + "] job data guids related to the job [" + jobSnapshot.getGuid() + "] - was expecting 1");
    }

    String lastModifiedValue = DateTimeFormatter.RFC_1123_DATE_TIME
            .format(ZonedDateTime.ofInstant(lastModifyTimestamp.toInstant(), ZoneOffset.UTC));
    String destinationLocationUrl = UriComponentsBuilder.newInstance()
            .pathSegment(AuthenticationFilter.SEGMENT_SECURED).pathSegment(JobController.SEGMENT_JOBDATA)
            .pathSegment(jobDataGuids.iterator().next()).pathSegment(JobController.SEGMENT_DOWNLOAD)
            .toUriString();

    response.addHeader(HttpHeaders.LAST_MODIFIED, lastModifiedValue);
    response.sendRedirect(destinationLocationUrl);
}

From source file:org.openlmis.auth.service.BaseCommunicationService.java

protected URI buildUri(String url, Map<String, ?> params) {
    UriComponentsBuilder builder = UriComponentsBuilder.newInstance().uri(URI.create(url));

    params.entrySet().forEach(e -> builder.queryParam(e.getKey(), e.getValue()));

    return builder.build(true).toUri();
}

From source file:org.openmrs.module.radiology.dicom.DicomWebViewer.java

/**
 * Return URL to open DICOM web viewer for given Study.
 * //  ww  w  .  ja v  a2  s.  c o m
 * @param study Study for which DICOM web viewer URL should be created
 * @throws IllegalArgumentException given null
 * @throws IllegalArgumentException given a study with studyInstanceUid null
 * @should return a url to open dicom images of the given study in the configured dicom viewer
 * @should add query param server name to url if local server name is not blank
 * @should throw an illegal argument exception given null
 * @should throw an illegal argument exception given study with studyInstanceUid null
 */
public String getDicomViewerUrl(Study study) {
    if (study == null) {
        throw new IllegalArgumentException("study cannot be null");
    } else if (study.getStudyInstanceUid() == null) {
        throw new IllegalArgumentException("studyInstanceUid cannot be null");
    }

    final UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.newInstance().scheme("http")
            .host(radiologyProperties.getDicomWebViewerAddress())
            .port(Integer.valueOf(radiologyProperties.getDicomWebViewerPort()))
            .path(radiologyProperties.getDicomWebViewerBaseUrl())
            .queryParam("studyUID", study.getStudyInstanceUid());

    final String serverName = radiologyProperties.getDicomWebViewerLocalServerName();
    if (StringUtils.isNotBlank(serverName)) {
        uriComponentsBuilder.queryParam("serverName", serverName);
    }

    return uriComponentsBuilder.buildAndExpand().encode().toString();
}

From source file:org.springframework.security.oauth2.provider.endpoint.AuthorizationEndpoint.java

private String append(String base, Map<String, ?> query, Map<String, String> keys, boolean fragment) {

    UriComponentsBuilder template = UriComponentsBuilder.newInstance();
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(base);
    URI redirectUri;//from  w w  w  .  j a v a2  s  . co  m
    try {
        // assume it's encoded to start with (if it came in over the wire)
        redirectUri = builder.build(true).toUri();
    } catch (Exception e) {
        // ... but allow client registrations to contain hard-coded non-encoded values
        redirectUri = builder.build().toUri();
        builder = UriComponentsBuilder.fromUri(redirectUri);
    }
    template.scheme(redirectUri.getScheme()).port(redirectUri.getPort()).host(redirectUri.getHost())
            .userInfo(redirectUri.getUserInfo()).path(redirectUri.getPath());

    if (fragment) {
        StringBuilder values = new StringBuilder();
        if (redirectUri.getFragment() != null) {
            String append = redirectUri.getFragment();
            values.append(append);
        }
        for (String key : query.keySet()) {
            if (values.length() > 0) {
                values.append("&");
            }
            String name = key;
            if (keys != null && keys.containsKey(key)) {
                name = keys.get(key);
            }
            values.append(name + "={" + key + "}");
        }
        if (values.length() > 0) {
            template.fragment(values.toString());
        }
        UriComponents encoded = template.build().expand(query).encode();
        builder.fragment(encoded.getFragment());
    } else {
        for (String key : query.keySet()) {
            String name = key;
            if (keys != null && keys.containsKey(key)) {
                name = keys.get(key);
            }
            template.queryParam(name, "{" + key + "}");
        }
        template.fragment(redirectUri.getFragment());
        UriComponents encoded = template.build().expand(query).encode();
        builder.query(encoded.getQuery());
    }

    return builder.build().toUriString();

}

From source file:org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.java

private static UriComponentsBuilder fromMethodInternal(@Nullable UriComponentsBuilder baseUrl,
        Class<?> controllerType, Method method, Object... args) {

    baseUrl = getBaseUrlToUse(baseUrl);//from  w ww  .ja v  a2 s  . com
    String typePath = getTypeRequestMapping(controllerType);
    String methodPath = getMethodRequestMapping(method);
    String path = pathMatcher.combine(typePath, methodPath);
    baseUrl.path(path);
    UriComponents uriComponents = applyContributors(baseUrl, method, args);
    return UriComponentsBuilder.newInstance().uriComponents(uriComponents);
}