List of usage examples for org.springframework.web.util UriComponentsBuilder build
public UriComponents build()
From source file:org.cloudfoundry.identity.client.UaaContextFactory.java
/** * Returns the URI//from w w w .j a va2 s . co m * @return */ public URI getTokenUri() { UriComponentsBuilder tokenURI = UriComponentsBuilder.newInstance(); tokenURI.uri(uaaURI); tokenURI.path(tokenPath); return tokenURI.build().toUri(); }
From source file:com.github.ibm.domino.client.BaseClient.java
protected URI getUri(String pathParam) { StringBuilder baseUrl = new StringBuilder(); if (!"http".equals(protocol) && !"https".equals(protocol)) { protocol = "http"; }/* w w w. ja v a 2 s. c o m*/ baseUrl.append(protocol).append("://").append(host); if (port > 0) { baseUrl.append(":").append(port); } if (!path.startsWith("/")) { baseUrl.append("/"); } UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(baseUrl.toString()) .path(path + (pathParam != null && !pathParam.isEmpty() ? "/" + pathParam : "")); parameters.entrySet().stream().forEach((parameter) -> { builder.queryParam(parameter.getKey(), parameter.getValue()); }); return builder.build().toUri(); }
From source file:org.obiba.mica.user.UserProfileService.java
private String getProfileServiceUrlByApp(String username, String application, String group) { UriComponentsBuilder urlBuilder = UriComponentsBuilder.fromHttpUrl(getAgateUrl()).path(DEFAULT_REST_PREFIX); if (Strings.isNullOrEmpty(username)) { urlBuilder.path(String.format("/application/%s/users", application)); } else {/*from w w w . j a va2 s .c o m*/ urlBuilder.path(String.format("/application/%s/user/%s", application, username)); } if (!Strings.isNullOrEmpty(group)) { urlBuilder.queryParam("group", group); } return urlBuilder.build().toUriString(); }
From source file:org.appverse.web.framework.backend.test.util.oauth2.tests.predefined.authorizationcode.Oauth2AuthorizationCodeFlowPredefinedTests.java
@Test public void oauth2FlowTest() throws Exception { // Obtains the token obtainTokenFromOuth2LoginEndpoint(); // Call remotelog ResponseEntity<String> result = callRemoteLogWithAccessToken(); assertEquals(HttpStatus.OK, result.getStatusCode()); // Call remotelog once the access token has expired (we wait enough to make sure it has expired) Thread.sleep(getTokenExpirationDelayInSeconds() * 1000); // Call remotelog result = callRemoteLogWithAccessToken(); assertEquals(HttpStatus.UNAUTHORIZED, result.getStatusCode()); assertTrue(result.getBody().contains("Access token expired")); // Refresh the token refreshToken();/*from w w w . j a v a 2s . c om*/ if (!isJwtTokenStore) { // The following code is executed only if the token store is not a JwtTokenStore. The reason is that using this kind of store // the tokens can't be revoked (they just expire) and so this part of the test would fail. // A JwtTokenStore is not a proper store as the tokens are not stored anywhere (as they contain all the required info about the user // themselves. That's why the token revocation is not possible. // We call logout endpoint (we need to use the access token for this) UriComponentsBuilder builder = UriComponentsBuilder .fromHttpUrl(resourceServerBaseUrl + baseApiPath + oauth2LogoutEndpointPath); builder.queryParam("access_token", accessToken); ResponseEntity<String> result2 = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.POST, null, String.class); assertEquals(HttpStatus.OK, result2.getStatusCode()); // We try to call the protected API again (after having logged out which removes the token) - We expect not to be able to call the service. // This will throw a exception. In this case here in the test we receive an exception but really what happened was 'access denied' // A production client will receive the proper http error result = callRemoteLogWithAccessToken(); assertEquals(HttpStatus.UNAUTHORIZED, result.getStatusCode()); } }
From source file:org.openmhealth.shim.misfit.MisfitShim.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", Joiner.on(',').join(resource.getScope())) .queryParam("redirect_uri", getCallbackUrl()); return uriBuilder.build().encode().toUriString(); }
From source file:org.cloudfoundry.identity.client.UaaContextFactory.java
/** * Returns the authorize URI//from w w w .j a va2s . 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:de.blizzy.documentr.markdown.macro.impl.FlattrMacro.java
@Override public String getHtml(IMacroContext macroContext) { try {/*from w w w . ja v a 2 s . c o m*/ IMacroSettings settings = macroContext.getSettings(); String userId = settings.getSetting("userId"); //$NON-NLS-1$ if (StringUtils.isNotBlank(userId)) { HtmlSerializerContext htmlSerializerContext = macroContext.getHtmlSerializerContext(); String projectName = htmlSerializerContext.getProjectName(); String branchName = htmlSerializerContext.getBranchName(); String path = htmlSerializerContext.getPagePath(); String pageUri = htmlSerializerContext.getPageUri(path); String pageUrl = htmlSerializerContext.getUrl(pageUri); IPageStore pageStore = htmlSerializerContext.getPageStore(); Page page = pageStore.getPage(projectName, branchName, path, false); String title = page.getTitle(); String tags = StringUtils.join(page.getTags(), ","); //$NON-NLS-1$ // http://developers.flattr.net/auto-submit/ UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("https://flattr.com/submit/auto") //$NON-NLS-1$ .queryParam("user_id", userId) //$NON-NLS-1$ .queryParam("url", pageUrl) //$NON-NLS-1$ .queryParam("title", title) //$NON-NLS-1$ .queryParam("category", "text"); //$NON-NLS-1$ //$NON-NLS-2$ if (StringUtils.isNotBlank(tags)) { builder.queryParam("tags", tags); //$NON-NLS-1$ } String url = builder.build().encode(Charsets.UTF_8.name()).toUriString(); return "<a href=\"" + StringEscapeUtils.escapeHtml4(url) + "\">" + //$NON-NLS-1$ //$NON-NLS-2$ "<img src=\"https://api.flattr.com/button/flattr-badge-large.png\"/></a>"; //$NON-NLS-1$ } } catch (IOException e) { log.warn("error while rendering Flattr macro", e); //$NON-NLS-1$ } return null; }
From source file:org.springframework.data.web.config.EnableSpringDataWebSupportIntegrationTests.java
@Test // DATACMNS-630 public void createsProxyForInterfaceBasedControllerMethodParameter() throws Exception { WebApplicationContext applicationContext = WebTestUtils.createApplicationContext(SampleConfig.class); MockMvc mvc = MockMvcBuilders.webAppContextSetup(applicationContext).build(); UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("/proxy"); builder.queryParam("name", "Foo"); builder.queryParam("shippingAddresses[0].zipCode", "ZIP"); builder.queryParam("shippingAddresses[0].city", "City"); builder.queryParam("billingAddress.zipCode", "ZIP"); builder.queryParam("billingAddress.city", "City"); builder.queryParam("date", "2014-01-11"); mvc.perform(post(builder.build().toString())).// andExpect(status().isOk());/* w w w . j a v a2 s .co m*/ }
From source file:org.appverse.web.framework.backend.test.util.frontfacade.mvc.tests.predefined.BasicAuthEndPointsServiceEnabledPredefinedTests.java
@Test public void basicAuthenticationFlowTest() throws Exception { // Login first TestLoginInfo loginInfo = login();/*from www . j a v a 2s .c om*/ // Calling protected remotelog service RemoteLogRequestVO logRequestVO = new RemoteLogRequestVO(); logRequestVO.setMessage("Test mesage!"); logRequestVO.setLogLevel("DEBUG"); HttpHeaders headers = new HttpHeaders(); headers.set("Cookie", loginInfo.getJsessionid()); HttpEntity<RemoteLogRequestVO> entityRemotelog = new HttpEntity<RemoteLogRequestVO>(logRequestVO, headers); UriComponentsBuilder builder = UriComponentsBuilder .fromHttpUrl("http://localhost:" + port + baseApiPath + remoteLogEndpointPath); // Try without token first - It should be 'Forbidden' // http://springinpractice.com/2012/04/08/sending-cookies-with-resttemplate ResponseEntity<String> responseEntityRemotelog = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.POST, entityRemotelog, String.class); assertEquals(HttpStatus.FORBIDDEN, responseEntityRemotelog.getStatusCode()); // Try now with the CSRF token - It should work well // This implies passing JSESSIONID and CSRF Token headers.set(DEFAULT_CSRF_HEADER_NAME, loginInfo.getXsrfToken()); entityRemotelog = new HttpEntity<RemoteLogRequestVO>(logRequestVO, headers); responseEntityRemotelog = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.POST, entityRemotelog, String.class); assertEquals(HttpStatus.OK, responseEntityRemotelog.getStatusCode()); // Calling here logout builder = UriComponentsBuilder .fromHttpUrl("http://localhost:" + port + basicAuthenticationLogoutEndpointPath); HttpEntity<Void> entityLogout = new HttpEntity<Void>(headers); responseEntityRemotelog = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.POST, entityLogout, String.class); assertEquals(HttpStatus.OK, responseEntityRemotelog.getStatusCode()); // Try to call remotelog again (after logout) // This implies passing JSESSIONID and CSRF Token - We expect this not to work as the CSRF token has been removed and the session invalidated entityRemotelog = new HttpEntity<RemoteLogRequestVO>(logRequestVO, headers); responseEntityRemotelog = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.POST, entityRemotelog, String.class); assertEquals(HttpStatus.FORBIDDEN, responseEntityRemotelog.getStatusCode()); }
From source file:org.appverse.web.framework.backend.test.util.frontfacade.mvc.tests.predefined.BasicAuthEndPointsServiceEnabledPredefinedTests.java
@Test public void simpleAuthenticationFlowTest() throws Exception { // Login first TestLoginInfo loginInfo = login();/* w w w . j a va 2 s.c o m*/ // Calling protected remotelog service RemoteLogRequestVO logRequestVO = new RemoteLogRequestVO(); logRequestVO.setMessage("Test mesage!"); logRequestVO.setLogLevel("DEBUG"); HttpHeaders headers = new HttpHeaders(); headers.set("Cookie", loginInfo.getJsessionid()); HttpEntity<RemoteLogRequestVO> entityRemotelog = new HttpEntity<RemoteLogRequestVO>(logRequestVO, headers); UriComponentsBuilder builder = UriComponentsBuilder .fromHttpUrl("http://localhost:" + port + baseApiPath + remoteLogEndpointPath); // Try without token first - It should be 'Forbidden' // http://springinpractice.com/2012/04/08/sending-cookies-with-resttemplate ResponseEntity<String> responseEntityRemotelog = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.POST, entityRemotelog, String.class); assertEquals(HttpStatus.FORBIDDEN, responseEntityRemotelog.getStatusCode()); // Try now with the CSRF token - It should work well // This implies passing JSESSIONID and CSRF Token headers.set(DEFAULT_CSRF_HEADER_NAME, loginInfo.getXsrfToken()); entityRemotelog = new HttpEntity<RemoteLogRequestVO>(logRequestVO, headers); responseEntityRemotelog = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.POST, entityRemotelog, String.class); assertEquals(HttpStatus.OK, responseEntityRemotelog.getStatusCode()); // Calling here logout builder = UriComponentsBuilder .fromHttpUrl("http://localhost:" + port + basicAuthenticationLogoutEndpointPath); HttpEntity<Void> entityLogout = new HttpEntity<Void>(headers); responseEntityRemotelog = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.POST, entityLogout, String.class); assertEquals(HttpStatus.OK, responseEntityRemotelog.getStatusCode()); // Try to call remotelog again (after logout) // This implies passing JSESSIONID and CSRF Token - We expect this not to work as the CSRF token has been removed and the session invalidated entityRemotelog = new HttpEntity<RemoteLogRequestVO>(logRequestVO, headers); responseEntityRemotelog = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.POST, entityRemotelog, String.class); assertEquals(HttpStatus.FORBIDDEN, responseEntityRemotelog.getStatusCode()); }