@Before public void setup() { publisher = mock(Publisher.class); when(publisher.getContextType()).thenReturn(ContextType.COURSE); when(publisher.getContextId()).thenReturn(contextId); uriBuilderTestObject = new UriBuilder(); uriBuilderTestObject.setServerContextPathURI(testContextPathUri); Set<PublisherTypeHandler> typeHandler = new HashSet<PublisherTypeHandler>(); typeHandler.add(new ForumNotificationTypeHandler()); typeHandler.add(new WikiNotificationTypeHandler()); PublisherTypeHandlerSelector typeHandlerSelector = new PublisherTypeHandlerSelector(); typeHandlerSelector.notificationTypeHandler = typeHandler; uriBuilderTestObject.typeHandlerSelector = typeHandlerSelector; }
@Test public void getURIToContext_ContextType_UNKOWN() { Publisher publisherUnkown = mock(Publisher.class); when(publisherUnkown.getContextType()).thenReturn(ContextType.UNKNOWN); String uriToContext = uriBuilderTestObject.getURIToContext(publisherUnkown); assertEquals("Wrong URI with unkown type", testContextPathUri + "/url/UNKOWN/0", uriToContext); }
private void checkUriToContext() { when(publisher.getSubcontextId()).thenReturn(subContextId); String uriToEventSource = uriBuilderTestObject.getURIToEventSource(publisher); assertEquals( "Wrong URI with type " + publisher.getSourceType(), testContextPathUri + "/url/RepositoryEntry/" + contextId + "/CourseNode/" + subContextId, uriToEventSource); }
@Test public void getURIToContext_ContextType_COURSE() { String uriToContext = uriBuilderTestObject.getURIToContext(publisher); assertEquals( "Wrong URI to context", testContextPathUri + "/url/RepositoryEntry/" + contextId, uriToContext); }
private Response sendAuthorizationCodeResponse(AuthorizationRequest authReq) { String uri = authReq.getRedirectUri(); String authorizationCode = getAuthorizationCodeValue(); authReq.setAuthorizationCode(authorizationCode); authorizationRequestRepository.save(authReq); uri = uri + appendQueryMark(uri) + "code=" + authorizationCode + appendStateParameter(authReq); return Response.seeOther(UriBuilder.fromUri(uri).build()) .cacheControl(cacheControlNoStore()) .header("Pragma", "no-cache") .build(); }
Uri(UriBuilder builder) { scheme = builder.getScheme(); authority = builder.getAuthority(); path = builder.getPath(); query = builder.getQuery(); fragment = builder.getFragment(); queryParameters = Collections.unmodifiableMap(Maps.newLinkedHashMap(builder.getQueryParameters())); StringBuilder out = new StringBuilder(); if (scheme != null) { out.append(scheme).append(':'); } if (authority != null) { out.append("//").append(authority); // insure that there's a separator between authority/path if (path != null && path.length() > 1 && !path.startsWith("/")) { out.append("/"); } } if (path != null) { out.append(path); } if (query != null) { out.append('?').append(query); } if (fragment != null) { out.append('#').append(fragment); } text = out.toString(); }
/** * Derived from Harmony Resolves a given url relative to this url. Resolution rules are the same * as for {@code java.net.URI.resolve(URI)} */ public Uri resolve(Uri relative) { if (relative == null) { return null; } if (relative.isAbsolute()) { return relative; } UriBuilder result; if (StringUtils.isEmpty(relative.path) && relative.scheme == null && relative.authority == null && relative.query == null && relative.fragment != null) { // if the relative URI only consists of fragment, // the resolved URI is very similar to this URI, // except that it has the fragement from the relative URI. result = new UriBuilder(this); result.setFragment(relative.fragment); } else if (relative.scheme != null) { result = new UriBuilder(relative); } else if (relative.authority != null) { // if the relative URI has authority, // the resolved URI is almost the same as the relative URI, // except that it has the scheme of this URI. result = new UriBuilder(relative); result.setScheme(scheme); } else { // since relative URI has no authority, // the resolved URI is very similar to this URI, // except that it has the query and fragment of the relative URI, // and the path is different. result = new UriBuilder(this); result.setFragment(relative.fragment); result.setQuery(relative.query); String relativePath = (relative.path == null) ? "" : relative.path; if (relativePath.startsWith("/")) { // $NON-NLS-1$ result.setPath(relativePath); } else { // resolve a relative reference int endindex = path.lastIndexOf('/') + 1; result.setPath(normalizePath(path.substring(0, endindex) + relativePath)); } } Uri resolved = result.toUri(); validate(resolved); return resolved; }
@Test public void getURIToSourceEntry_Forum() { when(publisher.getSourceType()).thenReturn(ForumNotificationTypeHandler.FORUM_SOURCE_TYPE); when(publisher.getSubcontextId()).thenReturn(subContextId); String uriToSourceEntry = uriBuilderTestObject.getURIToSourceEntry(publisher, sourceEntryId); assertEquals( "Wrong URI with type " + publisher.getSourceType(), testContextPathUri + "/url/RepositoryEntry/" + contextId + "/CourseNode/" + subContextId + "/Message/" + sourceEntryId, uriToSourceEntry); }
private Response sendImplicitGrantResponse( AuthorizationRequest authReq, AccessToken accessToken) { String uri = authReq.getRedirectUri(); String fragment = String.format( "access_token=%s&token_type=bearer&expires_in=%s&scope=%s", accessToken.getToken(), accessToken.getExpiresIn(), StringUtils.join(authReq.getGrantedScopes(), ',')) + appendStateParameter(authReq); if (authReq.getClient().isIncludePrincipal()) { fragment += String.format("&principal=%s", authReq.getPrincipal().getDisplayName()); } return Response.seeOther(UriBuilder.fromUri(uri).fragment(fragment).build()) .cacheControl(cacheControlNoStore()) .header("Pragma", "no-cache") .build(); }
@Before public void setup() { uriBuilder.serverContextPathURI = "/test"; mailChannel.setMailService(mailServiceConcordionMock); notificationServiceImpl.notifyDelegate.channelChain.setMailChannel(mailChannel); }
public static void main(String[] args) { java.net.URI serviceURI = UriBuilder.fromUri("http://localhost:8080/jaxrs").build(); ClientConfig config = new DefaultClientConfig(); Client client = Client.create(config); WebResource weatherService = client.resource(serviceURI); String weatherResult = weatherService .path("Rest") .path("WeatherService") .path("Rain") .accept(MediaType.APPLICATION_JSON) .get(String.class); String weatherResult2 = weatherService .path("Rest") .path("WeatherService") .path("Temperature/94043") .accept(MediaType.APPLICATION_JSON) .get(String.class); String weatherResult3 = weatherService .path("Rest") .path("WeatherService") .path("Temperature/12345") .accept(MediaType.APPLICATION_JSON) .get(String.class); System.out.println(weatherResult); System.out.println(weatherResult2); System.out.println(weatherResult3); MultivaluedMap formData = new MultivaluedMapImpl(); formData.add("temperature", "25"); formData.add("humidity", "15%"); formData.add("rain", "false"); formData.add("zipCode", "94043"); String response = weatherService .path("Rest") .path("WeatherService") .type("application/x-www-form-urlencoded") .post(String.class, formData); System.out.println(response); String weatherResult4 = weatherService .path("Rest") .path("WeatherService") .path("WeatherData/94043") .accept(MediaType.APPLICATION_JSON) .get(String.class); System.out.println(weatherResult4); String weatherResult5 = weatherService .path("Rest") .path("WeatherService") .path("WeatherData/94043") .accept(MediaType.APPLICATION_XML) .get(String.class); System.out.println(weatherResult5); }
public UriBuilder buildUri(String url) { return UriBuilder.fromUri(url.startsWith("http:") ? url : getUrl(url)); }