/** {@inheritDoc} */ @Override public Collection<CacheEntity> getCaches(final UriInfo info) { LOG.debug( String.format("Invoking CachesResourceServiceImpl.getCaches: %s", info.getRequestUri())); validator.validateSafe(info); String cacheManagerNames = info.getPathSegments().get(1).getMatrixParameters().getFirst("names"); Set<String> cmNames = cacheManagerNames == null ? null : new HashSet<String>(Arrays.asList(cacheManagerNames.split(","))); String cacheNames = info.getPathSegments().get(2).getMatrixParameters().getFirst("names"); Set<String> cNames = cacheNames == null ? null : new HashSet<String>(Arrays.asList(cacheNames.split(","))); MultivaluedMap<String, String> qParams = info.getQueryParameters(); List<String> attrs = qParams.get(ATTR_QUERY_KEY); Set<String> cAttrs = attrs == null || attrs.isEmpty() ? null : new HashSet<String>(attrs); try { return entityResourceFactory.createCacheEntities(cmNames, cNames, cAttrs); } catch (ServiceExecutionException e) { throw new ResourceRuntimeException( "Failed to get caches", e, Response.Status.BAD_REQUEST.getStatusCode()); } }
/** Migrated Jersey 1.x {@code com.sun.jersey.impl.PathSegmentsHttpRequestTest}. */ @Test public void testGetPathSegmentsGeneral() { final UriInfo ui = createContext("/p1;x=1;y=1/p2;x=2;y=2/p3;x=3;y=3", "GET"); List<PathSegment> segments = ui.getPathSegments(); assertEquals(3, segments.size()); final Iterator<PathSegment> psi = segments.iterator(); PathSegment segment; segment = psi.next(); assertEquals("p1", segment.getPath()); MultivaluedMap<String, String> m = segment.getMatrixParameters(); assertEquals("1", m.getFirst("x")); assertEquals("1", m.getFirst("y")); segment = psi.next(); assertEquals("p2", segment.getPath()); m = segment.getMatrixParameters(); assertEquals("2", m.getFirst("x")); assertEquals("2", m.getFirst("y")); segment = psi.next(); assertEquals("p3", segment.getPath()); m = segment.getMatrixParameters(); assertEquals("3", m.getFirst("x")); assertEquals("3", m.getFirst("y")); }
/** * Set the cache control and last modified HTTP headers from data in the graph * * @param httpHeaders * @param rdf */ public static void setCachingHeaders( final MultivaluedMap<String, Object> httpHeaders, final Dataset rdf, final UriInfo uriInfo) { final List<PathSegment> segments = uriInfo.getPathSegments(); if (!segments.isEmpty() && segments.get(0).getPath().startsWith("tx:")) { // Do not set caching headers if we are in a transaction return; } httpHeaders.put(CACHE_CONTROL, of((Object) "max-age=0", (Object) "must-revalidate")); LOGGER.trace( "Attempting to discover the last-modified date of the node for the resource in question..."); final Iterator<Quad> iterator = rdf.asDatasetGraph().find(ANY, getDatasetSubject(rdf), lastModifiedPredicate, ANY); if (!iterator.hasNext()) { return; } final Object dateObject = iterator.next().getObject().getLiteralValue(); if (!(dateObject instanceof XSDDateTime)) { LOGGER.debug("Found last-modified date, but it was not an XSDDateTime: {}", dateObject); return; } final XSDDateTime lastModified = (XSDDateTime) dateObject; LOGGER.debug("Found last-modified date: {}", lastModified); final String lastModifiedAsRdf2822 = RFC2822DATEFORMAT.print(new DateTime(lastModified.asCalendar())); httpHeaders.put(LAST_MODIFIED, of((Object) lastModifiedAsRdf2822)); }
/** {@inheritDoc} */ @Override public void createOrUpdateCache(final UriInfo info, CacheEntity resource) { LOG.debug( String.format( "Invoking CachesResourceServiceImpl.createOrUpdateCache: %s", info.getRequestUri())); validator.validate(info); String cacheManagerName = info.getPathSegments().get(1).getMatrixParameters().getFirst("names"); String cacheName = info.getPathSegments().get(2).getMatrixParameters().getFirst("names"); try { cacheSvc.createOrUpdateCache(cacheManagerName, cacheName, resource); } catch (ServiceExecutionException e) { throw new ResourceRuntimeException( "Failed to create or update cache", e, Response.Status.BAD_REQUEST.getStatusCode()); } }
private boolean isMatch(DetailedLink link, UriInfo uriInfo, String httpMethod) { int baseUriLength = uriInfo.getBaseUri().getPath().length(); // e.g: [vms, {vm:id}, start] String[] linkPathSegments = link.getHref().substring(baseUriLength).split("/"); // e.g: [vms, f26b0918-8e16-4915-b1c2-7f39e568de23, start] List<PathSegment> uriPathSegments = uriInfo.getPathSegments(); return isMatchLength(linkPathSegments, uriPathSegments) && isMatchPath(linkPathSegments, uriPathSegments) && isMatchRel(link.getRel(), httpMethod); }
URI getLocation(UriInfo info, String id) { List<PathSegment> segments = info.getPathSegments(); List<PathSegment> patate = segments.subList(0, segments.size() - 1); StringBuilder root = new StringBuilder(); for (PathSegment s : patate) { root.append('/').append(s.getPath()); } root.append("/session"); return info.getBaseUriBuilder().path(root.toString()).path(id).build(); }
/** Migrated Jersey 1.x {@code com.sun.jersey.impl.PathSegmentsHttpRequestTest}. */ @Test public void testGetPathSegmentsMultipleMatrix() { final UriInfo ui = createContext("/p;x=1;x=2;x=3", "GET"); List<PathSegment> segments = ui.getPathSegments(); assertEquals(1, segments.size()); final Iterator<PathSegment> psi = segments.iterator(); PathSegment segment; segment = psi.next(); MultivaluedMap<String, String> m = segment.getMatrixParameters(); List<String> values = m.get("x"); for (int i = 0; i < m.size(); i++) { assertEquals(Integer.valueOf(i + 1).toString(), values.get(i)); } }
/** {@inheritDoc} */ @Override public void updateCacheManager(UriInfo info, CacheManagerEntity resource) { LOG.info(String.format("Invoking updateCacheManager: %s", info.getRequestUri())); validator.validate(info); String cacheManagerName = info.getPathSegments().get(1).getMatrixParameters().getFirst("names"); try { cacheMgrSvc.updateCacheManager(cacheManagerName, resource); } catch (ServiceExecutionException e) { LOG.error("Failed to update cache manager.", e.getCause()); throw new WebApplicationException( Response.status(Response.Status.BAD_REQUEST).entity(e.getCause().getMessage()).build()); } }
@GET @Produces("text/plain") public String get( @MatrixParam("firstname") String firstname, @MatrixParam("lastname") String lastname, @Context UriInfo uriInfo) { final List<PathSegment> pathSegents = uriInfo.getPathSegments(); final PathSegment lastPathSegm = pathSegents.get(0); final MultivaluedMap<String, String> mp = lastPathSegm.getMatrixParameters(); if (mp.isEmpty()) { final ResponseBuilder rb = Response.status(Status.NOT_FOUND); rb.entity("matrix parameters are empty"); throw new WebApplicationException(rb.build()); } return firstname + " " + lastname; }
protected Templates createTemplates( Templates templates, Map<String, Object> configuredParams, Map<String, String> outProps) { if (templates == null) { if (supportJaxbOnly) { return null; } else { LOG.severe("No template is available"); throw ExceptionUtils.toInternalServerErrorException(null, null); } } TemplatesImpl templ = new TemplatesImpl(templates, uriResolver); MessageContext mc = getContext(); if (mc != null) { UriInfo ui = mc.getUriInfo(); MultivaluedMap<String, String> params = ui.getPathParameters(); for (Map.Entry<String, List<String>> entry : params.entrySet()) { String value = entry.getValue().get(0); int ind = value.indexOf(";"); if (ind > 0) { value = value.substring(0, ind); } templ.setTransformerParameter(entry.getKey(), value); } List<PathSegment> segments = ui.getPathSegments(); if (segments.size() > 0) { setTransformParameters(templ, segments.get(segments.size() - 1).getMatrixParameters()); } setTransformParameters(templ, ui.getQueryParameters()); templ.setTransformerParameter(ABSOLUTE_PATH_PARAMETER, ui.getAbsolutePath().toString()); templ.setTransformerParameter(RELATIVE_PATH_PARAMETER, ui.getPath()); templ.setTransformerParameter(BASE_PATH_PARAMETER, ui.getBaseUri().toString()); if (configuredParams != null) { for (Map.Entry<String, Object> entry : configuredParams.entrySet()) { templ.setTransformerParameter(entry.getKey(), entry.getValue()); } } } if (outProps != null) { templ.setOutProperties(outProps); } return templ; }
/** {@inheritDoc} */ public Collection<CacheManagerEntity> getCacheManagers(UriInfo info) { LOG.info(String.format("Invoking getCacheManagers: %s", info.getRequestUri())); validator.validateSafe(info); String names = info.getPathSegments().get(1).getMatrixParameters().getFirst("names"); Set<String> cmNames = names == null ? null : new HashSet<String>(Arrays.asList(names.split(","))); MultivaluedMap<String, String> qParams = info.getQueryParameters(); List<String> attrs = qParams.get(ATTR_QUERY_KEY); Set<String> cmAttrs = attrs == null || attrs.isEmpty() ? null : new HashSet<String>(attrs); try { return entityResourceFactory.createCacheManagerEntities(cmNames, cmAttrs); } catch (ServiceExecutionException e) { LOG.error("Failed to get cache managers.", e.getCause()); throw new WebApplicationException( Response.status(Response.Status.BAD_REQUEST).entity(e.getCause().getMessage()).build()); } }
@Override public Collection<CacheManagerConfigEntity> getCacheManagerConfigs(UriInfo info) { LOG.debug( String.format( "Invoking CacheManagerConfigsResourceServiceImpl.getXMLCacheManagerConfigs: %s", info.getRequestUri())); validator.validateSafe(info); String names = info.getPathSegments().get(1).getMatrixParameters().getFirst("names"); Set<String> cmNames = names == null ? null : new HashSet<String>(Arrays.asList(names.split(","))); try { return entityResourceFactory.createCacheManagerConfigEntities(cmNames); } catch (ServiceExecutionException e) { throw new ResourceRuntimeException( "Failed to get xml cache manager configs", e, Response.Status.BAD_REQUEST.getStatusCode()); } }
/** Migrated Jersey 1.x {@code com.sun.jersey.impl.PathSegmentsHttpRequestTest}. */ @Test public void testGetPathSegmentsMultipleSlash() { final UriInfo ui = createContext("/p//p//p//", "GET"); List<PathSegment> segments = ui.getPathSegments(); assertEquals(7, segments.size()); final Iterator<PathSegment> psi = segments.iterator(); PathSegment segment; segment = psi.next(); assertEquals("p", segment.getPath()); assertEquals(0, segment.getMatrixParameters().size()); segment = psi.next(); assertEquals("", segment.getPath()); assertEquals(0, segment.getMatrixParameters().size()); segment = psi.next(); assertEquals("p", segment.getPath()); assertEquals(0, segment.getMatrixParameters().size()); segment = psi.next(); assertEquals("", segment.getPath()); assertEquals(0, segment.getMatrixParameters().size()); segment = psi.next(); assertEquals("p", segment.getPath()); assertEquals(0, segment.getMatrixParameters().size()); segment = psi.next(); assertEquals("", segment.getPath()); assertEquals(0, segment.getMatrixParameters().size()); segment = psi.next(); assertEquals("", segment.getPath()); assertEquals(0, segment.getMatrixParameters().size()); }