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:com.jvoid.quote.controller.JVoidQuoteController.java

public ProductsMaster getJVoidProduct(int productId) {

    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 a 2s  .  c  o m*/
        jsonObj.put("id", productId);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("param jsonObj=>" + jsonObj.toString());

    UriComponentsBuilder builder = UriComponentsBuilder
            .fromHttpUrl(PRODUCT_SERVER_URI + URIConstants.GET_PRODUCT).queryParam("params", jsonObj);
    HttpEntity<?> entity = new HttpEntity<>(headers);
    HttpEntity<String> returnString = restTemplate.exchange(builder.build().toUri(), HttpMethod.GET, entity,
            String.class);
    System.out.println("returnString=>" + returnString);

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

    JSONArray productsArr = null;
    ProductsMaster productsMaster = null;
    try {
        productsArr = returnJsonObj.getJSONArray("products");
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    productsMaster = new ProductsMaster();
    try {
        ObjectMapper mapper = new ObjectMapper();
        try {
            productsMaster = mapper.readValue(productsArr.getJSONObject(0).toString(), ProductsMaster.class);
        } catch (JsonParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return productsMaster;
}

From source file:org.appverse.web.framework.backend.test.util.oauth2.tests.predefined.implicit.Oauth2ImplicitFlowPredefinedTests.java

@Test
public void obtainTokenFromOuth2LoginEndpoint() throws Exception {
    UriComponentsBuilder builder = UriComponentsBuilder
            .fromHttpUrl(authServerBaseUrl + oauth2AuthorizeEndpointPath);
    builder.queryParam("username", getUsername());
    builder.queryParam("password", getPassword());
    builder.queryParam("client_id", getClientId());
    builder.queryParam("response_type", "token");
    builder.queryParam("redirect_uri", "http://anywhere");

    HttpEntity<String> entity = new HttpEntity<>("");
    ResponseEntity<String> result2 = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.POST,
            entity, String.class);

    // This means the user was correctly authenticated, then a redirection was performed to /oauth/authorize to obtain the token.
    // Then the token was sucessfully obtained (authenticating the client properly) and a last redirection was performed to the 
    // redirect_uri with the token after #
    assertEquals(HttpStatus.FOUND, result2.getStatusCode());

    // Obtain the token from redirection URL after #
    URI location = result2.getHeaders().getLocation();
    accessToken = extractToken(location.getFragment().toString());
    assertNotNull(accessToken);/*from  w ww  . j a  v  a  2  s  .  c o m*/

    if (!isJwtTokenStore) {
        // Temporarily we can't not apply the default token enhacer adding the authorities if we use a JwtTokenStore
        // TODO: Put again the login endpoint separated for CSRF and return the authorities there

        // Obtain the user credentials from redirection URL after #
        String extractUserAuthorities = extractUserAuthorities(location.getFragment().toString());
        assertNotNull(extractUserAuthorities);
    }
}

From source file:org.openmhealth.shim.googlefit.GoogleFitShim.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("access_type", "offline")
            .queryParam("approval_prompt", "force")
            .queryParam("scope", StringUtils.collectionToDelimitedString(resource.getScope(), " "))
            .queryParam("redirect_uri", getCallbackUrl());

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

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

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

    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
    UriComponentsBuilder builder = UriComponentsBuilder
            .fromHttpUrl(ServerUris.CUSTOMER_SERVER_URI + URIConstants.GET_CUSTOMER_BY_EMAIL)
            .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:com.jvoid.core.controller.HomeController.java

@RequestMapping("/jvoid-products")
public @ResponseBody String listAllJVoidProductsForOutView() {
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);

    JSONObject jsonObj = new JSONObject();
    try {/*  ww w .ja  v  a2s  .co m*/
        jsonObj.put("id", -1);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

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

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

    return returnJsonObj.toString();
}

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

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

    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
    UriComponentsBuilder builder = UriComponentsBuilder
            .fromHttpUrl(ServerUris.CUSTOMER_SERVER_URI + URIConstants.RESET_CUSTOMER_PASSWORD)
            .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:com.jvoid.core.controller.HomeController.java

@RequestMapping("/jvoid-categories")
public @ResponseBody String listAllJVoidCategoriesForOutView() {
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);

    JSONObject jsonObj = new JSONObject();
    try {/*from  w ww . jav  a 2  s.  co m*/
        jsonObj.put("id", -1);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

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

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

    return returnJsonObj.toString();
}

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

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

    JSONObject jsonObj = new JSONObject();
    try {// w w w  . java2s  .  c om
        jsonObj.put("cartId", Integer.parseInt(cartId));
        jsonObj.put("productId", Integer.parseInt(productId));
        jsonObj.put("attributeId", 1);
        jsonObj.put("quantity", 1);
    } 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.ADD_PRODUCT_TO_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("/jvoid-products-by-cat")
public @ResponseBody String listAllJVoidProductsByCategoryForOutView(@RequestParam("catId") String catId) {
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);

    JSONObject jsonObj = new JSONObject();
    try {/*from  www  .java  2s.c om*/
        jsonObj.put("id", Integer.parseInt(catId));
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

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

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

    return returnJsonObj.toString();
}

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

@RequestMapping(value = "/jvoid-checkout-cart", method = RequestMethod.POST)
public @ResponseBody String jvoidChrckoutCart(@RequestParam("params") String jsonParams) {
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);

    System.out.println("jsonParams=>" + jsonParams);
    UriComponentsBuilder builder = UriComponentsBuilder
            .fromHttpUrl(ServerUris.QUOTE_SERVER_URI + URIConstants.CHECKOUT_CART)
            .queryParam("params", jsonParams);
    HttpEntity<?> entity = new HttpEntity<>(headers);
    HttpEntity<String> returnString = restTemplate.exchange(builder.build().toUri(), HttpMethod.GET, entity,
            String.class);

    JSONObject returnJsonObj = null;//from   ww w  .ja  v a  2 s .c  om
    try {
        returnJsonObj = new JSONObject(returnString.getBody());
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("returnJsonObj=>" + returnJsonObj);

    String result = "";
    try {
        result = returnJsonObj.getString("result");
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    String response = "";
    if (result.equals("Success")) {
        UriComponentsBuilder builder1 = UriComponentsBuilder
                .fromHttpUrl(ServerUris.ORDER_SERVER_URI + URIConstants.ADD_ORDER)
                .queryParam("params", jsonParams);
        HttpEntity<?> entity1 = new HttpEntity<>(headers);
        HttpEntity<String> returnString1 = restTemplate.exchange(builder1.build().toUri(), HttpMethod.GET,
                entity1, String.class);
        response = returnString1.getBody();
    }

    return response;
}