public FakeSlingHttpServletRequest(ResourceResolver resolver, String scheme, String server, int port) { this.resource = new SyntheticResource(null, "", RESOURCE_TYPE); this.scheme = scheme; this.server = server; this.port = port; setMethod(null); }
@Override @SuppressWarnings("unchecked") public <Type> Type adaptTo(Class<Type> type) { if (type == ValueMap.class) { return (Type)this.properties; } else { return super.adaptTo(type); } }
@Override public Resource getChild(String relPath) { for (Resource resource : children) { // naive implementation that only covers the simplest-possible case to detect the correct child if (StringUtils.equals(resource.getName(), relPath)) { return resource; } } return super.getChild(relPath); }
/** * * @return the parent resource (might be a {@link NonExistingResource} in case the parent does not exist either). */ public Resource getParent() { Resource parent = super.getParent(); if (parent == null) { return new NonExistingResource(getResourceResolver(), ResourceUtil.getParent(getPath())); } else { return parent; } }
/** * Returns a resource for the given <code>path</code>. * If no resource exists at the given path a * <code>SyntheticResource</code> is returned. * * @param resolver The <code>ResourceResolver</code> used to access the * resource. * @param path The absolute path of the resource to return. * @return The actual resource at the given <code>path</code> or a * synthetic resource representing the path location. */ protected final Resource getResource(final ResourceResolver resolver, String path) { Resource res = resolver.getResource(path); if (res == null) { if (!path.startsWith("/")) { path = "/".concat(path); } res = new SyntheticResource(resolver, path, "$synthetic$"); } return res; }
@Override @SuppressWarnings("unchecked") public <Type> Type adaptTo(Class<Type> type) { if (type == String.class) { return (Type) ""; } return super.adaptTo(type); }
@Override public Resource getResource(ResolveContext<DoesNotNeedAContext> ctx, String path, ResourceContext resourceContext, Resource parent) { // Synthetic resource for our root, so that /planets works if((ROOT).equals(path)) { return new SyntheticResource(ctx.getResourceResolver(), path, PlanetResource.RESOURCE_TYPE); } // Not root, return a Planet if we have one final ValueMap data = PLANETS.get(path); return data == null ? null : new PlanetResource(ctx.getResourceResolver(), path, data); }
@SuppressWarnings("unchecked") @Override public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) { if (type == ValueMap.class) { return (AdapterType) properties; } return super.adaptTo(type); }
public MockSlingHttpServletRequest(String resourcePath, String selectors, String extension, String suffix, String queryString, String requestPath, String scheme, String server, int port, String contextPath) { this.resource = new SyntheticResource(null, resourcePath, RESOURCE_TYPE); this.requestPathInfo = new MockRequestPathInfo(selectors, extension, suffix, requestPath); this.queryString = queryString; this.scheme = scheme; this.server = server; this.port = port; this.contextPath = contextPath; setMethod(null); }
/** * {@inheritDoc} */ @Override @SuppressWarnings("unchecked") public final <AdapterType> AdapterType adaptTo(Class<AdapterType> type) { if (type == ValueMap.class || type == ModifiableValueMap.class) { return (AdapterType) this.data; } return super.adaptTo(type); } }
//Map<String, Object> jstlValues, final Map<String, Object> propertyValues, String resourceType, SlingHttpServletRequest request, SlingHttpServletResponse response final StringWriter outputBuffer = new StringWriter(); SlingHttpServletResponseWrapper responseWrapper = new SlingHttpServletResponseWrapper(response) { public PrintWriter getWriter() throws IOException { return new PrintWriter(outputBuffer); } public SlingHttpServletResponse getSlingResponse() { return super.getSlingResponse(); } }; ResourceMetadata resourceMetaData = new ResourceMetadata(); resourceMetaData.setResolutionPath("/dummy"); for (Map.Entry<String, Object> entry: jstlValues.entrySet()) { request.setAttribute(entry.getKey(), entry.getValue()); } SyntheticResource resource = new SyntheticResource(request.getResourceResolver(), resourceMetaData, resourceType) { public <T> T adaptTo(Class<T> type) { if (type == ValueMap.class) { ValueMap m = new ValueMapDecorator(propertyValues); return (T) m; } return super.adaptTo(type); } }; RequestDispatcher dispatcher = request.getRequestDispatcher(resource); dispatcher.include(request, responseWrapper); return outputBuffer.toString();
@SuppressWarnings("unchecked") @Override public <Type> Type adaptTo(Class<Type> type) { if (ValueMap.class.equals(type)) { return (Type)props; } else { return super.adaptTo(type); } }
public static RequestPathInfo getRequestPathInfo(String urlString) { if(urlString == null) { urlString = ""; } // For the path, take everything up to the first dot String fullPath = urlString; if(urlString.contains("http")) { try { fullPath = new URL(urlString).getPath(); } catch(MalformedURLException ignore) { } } final int firstDot = fullPath.indexOf("."); final ResourceMetadata metadata = new ResourceMetadata(); final Resource r = new SyntheticResource(null, metadata, null); metadata.setResolutionPath(firstDot < 0 ? fullPath : fullPath.substring(0, firstDot)); metadata.setResolutionPathInfo(firstDot < 0 ? null : fullPath.substring(firstDot)); return new SlingRequestPathInfo(r); } }
@SuppressWarnings("unchecked") @Override public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) { if (type == InputStream.class) { return (AdapterType) new ByteArrayInputStream(data.getBytes(charset)); } else if (type == ValueMap.class) { return (AdapterType) ValueMapDecorator.EMPTY; } else { return super.adaptTo(type); } }
return new SyntheticResource(resourceResolver, path, JcrConstants.NT_FOLDER); Resource resource = new SyntheticResource(resourceResolver, resourceMetaData, resourceType);
@Override @SuppressWarnings("unchecked") public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) { if (type == ValueMap.class) { if (valueMap == null) { initValueMap(); } return (AdapterType) valueMap; } else { return super.adaptTo(type); } }
public void setResourceResolver(ResourceResolver resolver) { this.mockResourceResolver = resolver; // recreate request resource with the new resolver if (resource.getResourceResolver() == null) { this.resource = new SyntheticResource(resolver, resource.getPath(), resource.getResourceType()); } }
/** * @see org.apache.sling.api.adapter.Adaptable#adaptTo(java.lang.Class) */ @SuppressWarnings("unchecked") public <AdapterType> AdapterType adaptTo(final Class<AdapterType> type) { if (type == ValueMap.class) { return (AdapterType) new ValueMapDecorator(values); } else if (type == Map.class) { return (AdapterType) values; } return super.adaptTo(type); }
return new SyntheticResource(context.getResourceResolver(), path, ResourceProvider.RESOURCE_TYPE_SYNTHETIC);
@Override @SuppressWarnings("unchecked") public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) { if (type.isAssignableFrom(ValueMap.class)) { return (AdapterType) getValueMap(); } return super.adaptTo(type); } }