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

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

Introduction

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

Prototype

public UriComponents build() 

Source Link

Document

Build a UriComponents instance from the various components contained in this builder.

Usage

From source file:org.openmhealth.shim.jawbone.JawboneShim.java

@Override
protected String getAuthorizationUrl(UserRedirectRequiredException exception) {

    final OAuth2ProtectedResourceDetails resource = getResource();

    UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(exception.getRedirectUri())
            .queryParam("state", exception.getStateKey()).queryParam("client_id", resource.getClientId())
            .queryParam("response_type", "code")
            .queryParam("scope", StringUtils.collectionToDelimitedString(resource.getScope(), " "))
            .queryParam("redirect_uri", getCallbackUrl());

    return uriBuilder.build().encode().toUriString();

}

From source file:org.devefx.httpmapper.binding.MapperMethod.java

private URI appendUrlParams(URI uri, MultiValueMap<String, Object> body) throws URISyntaxException {
    UriComponentsBuilder builder = UriComponentsBuilder.fromUri(uri);
    for (Map.Entry<String, List<Object>> entry : body.entrySet()) {
        String key = entry.getKey();
        for (Object value : entry.getValue()) {
            if (value instanceof String) {
                builder.queryParam(key, (String) value);
            }/*from w ww. j  a  v  a  2 s . c o m*/
        }
    }
    UriComponents uriComponents = builder.build();
    return uriComponents.toUri();
}

From source file:com.jvoid.core.controller.HomeController.java

@RequestMapping(value = "/sign-up", method = RequestMethod.POST)
public @ResponseBody String jvoidSignUpNewUser(
        @RequestParam(required = false, value = "params") JSONObject jsonParams) {
    System.out.println("sign-up:jsonParams=>" + jsonParams.toString());

    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
    String uri = "";
    try {//from   w w  w . ja  v a  2s. co  m
        if (jsonParams.getInt("id") > 0) {
            uri = ServerUris.CUSTOMER_SERVER_URI + URIConstants.UPDATE_CUSTOMER;
        } else {
            uri = ServerUris.CUSTOMER_SERVER_URI + URIConstants.ADD_CUSTOMER;
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(uri).queryParam("params", jsonParams);
    HttpEntity<?> entity = new HttpEntity<>(headers);
    HttpEntity<String> returnString = restTemplate.exchange(builder.build().toUri(), HttpMethod.GET, entity,
            String.class);
    return returnString.getBody();
}

From source file:org.openmhealth.shim.jawbone.JawboneShim.java

protected ResponseEntity<ShimDataResponse> getData(OAuth2RestOperations restTemplate,
        ShimDataRequest shimDataRequest) throws ShimException {

    final JawboneDataTypes jawboneDataType;
    try {//from   w w  w  . j  av  a  2 s . com
        jawboneDataType = JawboneDataTypes.valueOf(shimDataRequest.getDataTypeKey().trim().toUpperCase());
    } catch (NullPointerException | IllegalArgumentException e) {
        throw new ShimException("Null or Invalid data type parameter: " + shimDataRequest.getDataTypeKey()
                + " in shimDataRequest, cannot retrieve data.");
    }

    /*
    Jawbone defaults to returning a maximum of 10 entries per request (limit = 10 by default), so
    we override the default by specifying an arbitrarily large number as the limit.
     */
    long numToReturn = 100_000;

    OffsetDateTime today = OffsetDateTime.now();

    OffsetDateTime startDateTime = shimDataRequest.getStartDateTime() == null ? today.minusDays(1)
            : shimDataRequest.getStartDateTime();
    long startTimeInEpochSecond = startDateTime.toEpochSecond();

    // We are inclusive of the last day, so we need to add an extra day since we are dealing with start of day,
    // and would miss the activities that occurred during the last day within going to midnight of that day
    OffsetDateTime endDateTime = shimDataRequest.getEndDateTime() == null ? today.plusDays(1)
            : shimDataRequest.getEndDateTime().plusDays(1);
    long endTimeInEpochSecond = endDateTime.toEpochSecond();

    UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(DATA_URL)
            .path(jawboneDataType.getEndPoint()).queryParam("start_time", startTimeInEpochSecond)
            .queryParam("end_time", endTimeInEpochSecond).queryParam("limit", numToReturn);

    ResponseEntity<JsonNode> responseEntity;
    try {
        responseEntity = restTemplate.getForEntity(uriComponentsBuilder.build().encode().toUri(),
                JsonNode.class);
    } catch (HttpClientErrorException | HttpServerErrorException e) {
        // FIXME figure out how to handle this
        logger.error("A request for Jawbone data failed.", e);
        throw e;
    }

    if (shimDataRequest.getNormalize()) {

        JawboneDataPointMapper mapper;
        switch (jawboneDataType) {
        case WEIGHT:
            mapper = new JawboneBodyWeightDataPointMapper();
            break;
        case STEPS:
            mapper = new JawboneStepCountDataPointMapper();
            break;
        case BODY_MASS_INDEX:
            mapper = new JawboneBodyMassIndexDataPointMapper();
            break;
        case ACTIVITY:
            mapper = new JawbonePhysicalActivityDataPointMapper();
            break;
        case SLEEP:
            mapper = new JawboneSleepDurationDataPointMapper();
            break;
        case HEART_RATE:
            mapper = new JawboneHeartRateDataPointMapper();
            break;
        default:
            throw new UnsupportedOperationException();
        }

        return ResponseEntity.ok().body(ShimDataResponse.result(JawboneShim.SHIM_KEY,
                mapper.asDataPoints(singletonList(responseEntity.getBody()))));

    } else {

        return ResponseEntity.ok()
                .body(ShimDataResponse.result(JawboneShim.SHIM_KEY, responseEntity.getBody()));
    }

}

From source file:it.scoppelletti.wui.AboutAction.java

@Override
public String execute() {
    String uri;/* w w w .  ja v a2s.  c  o  m*/
    UriComponentsBuilder uriBuilder;
    ApplicationInfo about;
    ApplicationInfoResource aboutRes;
    List<String> applList;
    List<ApplicationInfo> aboutList;

    aboutList = new ArrayList<ApplicationInfo>();
    applList = myApplMgr.listRunningApplications();
    for (String ctxPath : applList) {
        uriBuilder = UriComponentsBuilder.fromHttpUrl(myApplMgr.getBaseUrl());
        uriBuilder.path(ctxPath).path(ApplicationInfoResource.PATH);
        uriBuilder.queryParam(AbstractServerResource.QUERY_LOCALE, getLocale().toString());

        uri = uriBuilder.build().toUriString();
        aboutRes = ClientResource.create(uri, ApplicationInfoResource.class);
        try {
            about = aboutRes.getApplicationInfo();
        } catch (Exception ex) {
            myLogger.error(String.format("Failed to get %1$s.", uri), ex);
            about = new SimpleApplicationInfo(ctxPath);
        }
        if (about == null) {
            myLogger.error("Failed to get {}.", uri);
            about = new SimpleApplicationInfo(ctxPath);
        }

        aboutList.add(about);
    }

    Collections.sort(aboutList, new ApplicationInfoComparator());
    myApplList = aboutList;

    return Action.SUCCESS;
}

From source file:it.scoppelletti.wui.PromptDialogAction.java

@Override
public String execute() {
    String uri;/*from  ww  w  . j a v a  2 s  .  c o  m*/
    UriComponentsBuilder uriBuilder;

    uri = getViewState().get(PromptDialogAction.VIEWSTATE_ACTIONURL);
    if (Strings.isNullOrEmpty(uri)) {
        throw new PropertyNotSetException(toString(), PromptDialogAction.VIEWSTATE_ACTIONURL);
    }

    uriBuilder = UriComponentsBuilder.fromHttpUrl(uri);
    uriBuilder.queryParam("closingDialogId", getViewState().get(ActionBase.VIEWSTATE_VIEWID));
    for (Map.Entry<String, String> param : getCallerState().entrySet()) {
        uriBuilder.queryParam(PromptDialogAction.VIEWSTATE_PREFIX.concat(param.getKey()), param.getValue());
    }

    myActionUrl = uriBuilder.build().toUriString();
    myLogger.info("User accepted action {}.", myActionUrl);

    return Action.SUCCESS;
}

From source file:com.jvoid.core.controller.HomeController.java

@RequestMapping("/cart")
public @ResponseBody String getCartById(@RequestParam("cartId") String cartId) {
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);

    JSONObject jsonObj = new JSONObject();
    try {/*w ww .  j  a  va  2  s  .co  m*/
        jsonObj.put("cartId", Integer.parseInt(cartId));
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("param jsonObj=>" + jsonObj.toString());

    UriComponentsBuilder builder = UriComponentsBuilder
            .fromHttpUrl(ServerUris.QUOTE_SERVER_URI + URIConstants.GET_CART).queryParam("params", jsonObj);
    HttpEntity<?> entity = new HttpEntity<>(headers);
    HttpEntity<String> returnString = restTemplate.exchange(builder.build().toUri(), HttpMethod.GET, entity,
            String.class);
    return returnString.getBody();
}

From source file:com.jvoid.core.controller.HomeController.java

@RequestMapping("/login-tester")
public @ResponseBody String jvoidLoginTester(@RequestParam("params") String jsonParams) {
    System.out.println("login-tester: jsonParams=>" + jsonParams);

    JSONObject jsonObj = null;/*  w  ww.j av  a  2s. co  m*/
    try {
        jsonObj = new JSONObject(jsonParams);
    } catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    System.out.println("Login-tester:jsonObj=>" + jsonObj);

    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);

    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:9080/jvoidcore/login")
            .queryParam("params", jsonObj);
    HttpEntity<?> entity = new HttpEntity<>(headers);
    HttpEntity<String> returnString = restTemplate.exchange(builder.build().toUri(), HttpMethod.POST, entity,
            String.class);
    System.out.println("returnString=>" + returnString);

    JSONObject returnJsonObj = null;
    try {
        returnJsonObj = new JSONObject();
        returnJsonObj.put("result", returnString);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return returnJsonObj.toString();
}

From source file:com.jvoid.core.controller.HomeController.java

@RequestMapping("/remove-cart")
public @ResponseBody String removeItemsFromCartById(@RequestParam("cartId") String cartId,
        @RequestParam("prodId") String prodId) {
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);

    JSONObject jsonObj = new JSONObject();
    try {//from   w w  w .j av a2  s. c o m
        jsonObj.put("cartId", Integer.parseInt(cartId));
        jsonObj.put("productId", Integer.parseInt(prodId));
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("param jsonObj=>" + jsonObj.toString());

    UriComponentsBuilder builder = UriComponentsBuilder
            .fromHttpUrl(ServerUris.QUOTE_SERVER_URI + URIConstants.DELETE_CART).queryParam("params", jsonObj);
    HttpEntity<?> entity = new HttpEntity<>(headers);
    HttpEntity<String> returnString = restTemplate.exchange(builder.build().toUri(), HttpMethod.GET, entity,
            String.class);
    return returnString.getBody();
}

From source file:it.scoppelletti.wui.ActivityPanelAction.java

/**
 * Elenco delle categorie.//  w  w w. j  av  a 2s  .co m
 * 
 * @param  applList Applicazioni.
 * @return          Collezione.
 */
private List<String> listCategories(List<String> applList) {
    String uri;
    UriComponentsBuilder uriBuilder;
    ActivityCategoryListResource catgRes;
    List<String> categoryList, list;
    Map<String, String> map;

    map = new HashMap<String, String>();
    for (String ctxPath : applList) {
        uriBuilder = UriComponentsBuilder.fromHttpUrl(myApplMgr.getBaseUrl());
        uriBuilder.path(ctxPath).path(ActivityCategoryListResource.PATH);
        uriBuilder.queryParam(AbstractServerResource.QUERY_LOCALE, getLocale().toString());

        uri = uriBuilder.build().toUriString();
        catgRes = ClientResource.create(uri, ActivityCategoryListResource.class);
        try {
            list = catgRes.listCategories();
        } catch (Exception ex) {
            myLogger.error(String.format("Failed to get %1$s.", uri), ex);
            continue;
        }
        if (list == null) {
            myLogger.error("Failed to get {}.", uri);
            continue;
        }

        for (String catg : list) {
            addCategory(map, Strings.trim(catg));
        }
    }

    categoryList = new ArrayList<String>(map.values());
    Collections.sort(categoryList);

    return categoryList;
}