@Test public void testInexistentHeader() { Map<String, List<String>> headers = new HashMap<>(); List<String> singleElementList = new ArrayList<>(); singleElementList.add("value0"); headers.put("header0", singleElementList); RequestContext ctx = new RequestContext("\basepath", "\requesturi", null, headers); assertTrue(ctx.getHeaders("header1").isEmpty()); assertNull(ctx.getFirstHeader("header1")); }
@Test public void testSingleValueHeader() { Map<String, List<String>> headers = new HashMap<>(); List<String> singleElementList = new ArrayList<>(); singleElementList.add("value0"); headers.put("header0", singleElementList); RequestContext ctx = new RequestContext("\basepath", "\requesturi", null, headers); assertEquals(1, ctx.getHeaders("header0").size()); assertEquals("value0", ctx.getHeaders("header0").get(0)); assertEquals("value0", ctx.getFirstHeader("header0")); }
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { final HttpServletRequest servletRequest = (HttpServletRequest) request; String requestURI = servletRequest.getRequestURI(); requestURI = StringUtils.removeStart( requestURI, servletRequest.getContextPath() + servletRequest.getServletPath()); String baseURL = StringUtils.removeEnd(servletRequest.getRequestURL().toString(), requestURI); Map<String, List<String>> headersMap = new HashMap<>(); Enumeration<String> headerNames = servletRequest.getHeaderNames(); if (headerNames != null) { while (headerNames.hasMoreElements()) { String headerName = headerNames.nextElement(); List<String> valuesList = Collections.list(servletRequest.getHeaders(headerName)); headersMap.put(headerName, valuesList); } } RequestContext ctx; Principal userPrincipal = servletRequest.getUserPrincipal(); if (userPrincipal != null) { ctx = new RequestContext( baseURL, servletRequest.getRequestURI(), servletRequest.getHeader(RequestContext.HATEOAS_OPTIONS_HEADER), userPrincipal, headersMap); } else { ctx = new RequestContext( baseURL, servletRequest.getRequestURI(), servletRequest.getHeader(RequestContext.HATEOAS_OPTIONS_HEADER), headersMap); } RequestContext.setRequestContext(ctx); try { chain.doFilter(request, response); } finally { RequestContext.clearRequestContext(); } }
@Test public void testGetFirstHeaderCaseInsensitive() { Map<String, List<String>> headers = new HashMap<>(); List<String> list = new ArrayList<>(); list.add("value0"); list.add("value1"); list.add("value2"); headers.put("header0", list); RequestContext ctx = new RequestContext("\basepath", "\requesturi", null, headers); assertNull(ctx.getFirstHeaderCaseInsensitive(null)); assertEquals("value0", ctx.getFirstHeaderCaseInsensitive("header0")); assertEquals("value0", ctx.getFirstHeaderCaseInsensitive("HEADER0")); assertEquals("value0", ctx.getFirstHeaderCaseInsensitive("Header0")); }
@Test public void testMultiValueHeader() { Map<String, List<String>> headers = new HashMap<>(); List<String> list = new ArrayList<>(); list.add("value0"); list.add("value1"); list.add("value2"); headers.put("header0", list); RequestContext ctx = new RequestContext("\basepath", "\requesturi", null, headers); assertEquals(3, ctx.getHeaders("header0").size()); assertEquals("value0", ctx.getHeaders("header0").get(0)); assertEquals("value1", ctx.getHeaders("header0").get(1)); assertEquals("value2", ctx.getHeaders("header0").get(2)); assertEquals("value0", ctx.getFirstHeader("header0")); }
/* * Using the supplied EntityResource, add the embedded resources * from the OEntity embedded resources. NB - only an OEntity can * carry OLinks. */ public void addExpandedLinks(EntityResource<OEntity> entityResource) { RequestContext requestContext = RequestContext.getRequestContext(); Collection<Link> links = entityResource.getLinks(); if (links != null) { OEntity oentity = entityResource.getEntity(); List<OLink> olinks = oentity.getLinks(); for (OLink olink : olinks) { if (olink.isInline()) { String relid = InternalUtil.getEntityRelId(oentity); String href = relid + "/" + olink.getTitle(); for (Link link : links) { String linkHref = link.getHref(); if (requestContext != null) { // Extract the transition fragment from the URI path linkHref = link.getRelativeHref(getBaseUri(serviceDocument, uriInfo)); } if (href.equals(linkHref)) { if (entityResource.getEmbedded() == null) { entityResource.setEmbedded(new HashMap<Transition, RESTResource>()); } if (olink.isCollection()) { List<OEntity> oentities = olink.getRelatedEntities(); Collection<EntityResource<OEntity>> entityResources = new ArrayList<EntityResource<OEntity>>(); for (OEntity oe : oentities) { entityResources.add(new EntityResource<OEntity>(oe)); } entityResource .getEmbedded() .put(link.getTransition(), new CollectionResource<OEntity>(entityResources)); } else { // replace the OLink's on the current entity OEntity inlineOentity = olink.getRelatedEntity(); List<OLink> inlineResourceOlinks = formOLinks(new EntityResource<OEntity>(inlineOentity)); OEntity newInlineOentity = OEntities.create( inlineOentity.getEntitySet(), inlineOentity.getEntityKey(), inlineOentity.getProperties(), inlineResourceOlinks); entityResource .getEmbedded() .put(link.getTransition(), new EntityResource<OEntity>(newInlineOentity)); } } } } } } }
private void addLinkToOLinks(List<OLink> olinks, Link link, RESTResource resource) { RequestContext requestContext = RequestContext.getRequestContext(); assert (link != null); assert (link.getTransition() != null); Map<Transition, RESTResource> embeddedResources = resource.getEmbedded(); String rel = link.getRel(); String href = link.getHref(); if (requestContext != null) { // Extract the transition fragment from the URI path href = link.getRelativeHref(getBaseUri(serviceDocument, uriInfo)); } String title = link.getTitle(); OLink olink = null; Transition linkTransition = link.getTransition(); if (linkTransition != null) { if (embeddedResources != null && embeddedResources.get(linkTransition) != null && embeddedResources.get(linkTransition) instanceof EntityResource) { @SuppressWarnings("unchecked") EntityResource<OEntity> embeddedResource = (EntityResource<OEntity>) embeddedResources.get(linkTransition); // replace the OLink's on the embedded entity OEntity newEmbeddedEntity = processOEntity(embeddedResource); olink = OLinks.relatedEntityInline(rel, title, href, newEmbeddedEntity); } else if (embeddedResources != null && embeddedResources.get(linkTransition) != null && embeddedResources.get(linkTransition) instanceof CollectionResource) { @SuppressWarnings("unchecked") CollectionResource<OEntity> embeddedCollectionResource = (CollectionResource<OEntity>) embeddedResources.get(linkTransition); List<OEntity> entities = new ArrayList<OEntity>(); for (EntityResource<OEntity> embeddedResource : embeddedCollectionResource.getEntities()) { // replace the OLink's on the embedded entity OEntity newEmbeddedEntity = processOEntity(embeddedResource); entities.add(newEmbeddedEntity); } olink = OLinks.relatedEntitiesInline(rel, title, href, entities); } if (olink == null) { if (linkTransition.getTarget() instanceof CollectionResourceState) { olink = OLinks.relatedEntities(rel, title, href); } else { olink = OLinks.relatedEntity(rel, title, href); } } } olinks.add(olink); }
@Before public void setup() { // initialise the thread local request context with requestUri and baseUri RequestContext ctx = new RequestContext("http://localhost/myservice.svc", "/baseuri/", null); RequestContext.setRequestContext(ctx); }