use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class JspServletWrapper method service.
/**
* @param bindings
* @throws SlingIOException
* @throws SlingServletException
* @throws IllegalArgumentException if the Jasper Precompile controller
* request parameter has an illegal value.
*/
public void service(final SlingBindings bindings) {
final SlingHttpServletRequest request = bindings.getRequest();
final Object oldValue = request.getAttribute(SlingBindings.class.getName());
try {
request.setAttribute(SlingBindings.class.getName(), bindings);
service(request, bindings.getResponse());
} catch (SlingException se) {
// rethrow as is
throw se;
} catch (IOException ioe) {
throw new SlingIOException(ioe);
} catch (ServletException se) {
throw new SlingServletException(se);
} finally {
request.setAttribute(SlingBindings.class.getName(), oldValue);
}
}
use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class StreamingUploadOperationTest method testPartsContentRange.
@Test
public void testPartsContentRange() throws PersistenceException, RepositoryException, UnsupportedEncodingException {
List<Modification> changes = new ArrayList<>();
PostResponse response = new AbstractPostResponse() {
@Override
protected void doSend(HttpServletResponse response) throws IOException {
}
@Override
public void onChange(String type, String... arguments) {
}
@Override
public String getPath() {
return "/test/upload/location";
}
};
List<Part> partsList = new ArrayList<>();
partsList.add(new MockPart("formfield1", null, null, 0, new ByteArrayInputStream("testformfield1".getBytes("UTF-8")), Collections.EMPTY_MAP));
partsList.add(new MockPart("formfield2", null, null, 0, new ByteArrayInputStream("testformfield2".getBytes("UTF-8")), Collections.EMPTY_MAP));
partsList.add(new MockPart("test1.txt", "text/plain", "test1bad.txt", 4, new ByteArrayInputStream("test".getBytes("UTF-8")), mapOf("Content-Range", "bytes 0-3/8", "Content-Length", "4")));
partsList.add(new MockPart("test1.txt", "text/plain", "test1bad.txt", 4, new ByteArrayInputStream("part".getBytes("UTF-8")), mapOf("Content-Range", "bytes 4-7/8", "Content-Length", "4")));
partsList.add(new MockPart("*", "text/plain2", "test2.txt", 8, new ByteArrayInputStream("test1234".getBytes("UTF-8")), Collections.EMPTY_MAP));
partsList.add(new MockPart("badformfield2", null, null, 0, new ByteArrayInputStream("testbadformfield2".getBytes("UTF-8")), Collections.EMPTY_MAP));
final Iterator<Part> partsIterator = partsList.iterator();
final Map<String, Resource> repository = new HashMap<>();
final ResourceResolver resourceResolver = new MockResourceResolver() {
@Override
public Resource getResource(String path) {
Resource resource = repository.get(path);
if (resource == null) {
if ("/test/upload/location".equals(path)) {
resource = new MockRealResource(this, path, "sling:Folder");
repository.put(path, resource);
LOG.debug("Created {} ", path);
}
}
LOG.debug("Resource {} is {} {}", path, resource, ResourceUtil.isSyntheticResource(resource));
return resource;
}
@Override
public Iterable<Resource> getChildren(Resource resource) {
List<Resource> children = new ArrayList<>();
for (Map.Entry<String, Resource> e : repository.entrySet()) {
if (isChild(resource.getPath(), e.getKey())) {
children.add(e.getValue());
}
}
return children;
}
private boolean isChild(String path, String key) {
if (key.length() > path.length() && key.startsWith(path)) {
return !key.substring(path.length() + 1).contains("/");
}
return false;
}
@Override
public Iterator<Resource> listChildren(Resource parent) {
return getChildren(parent).iterator();
}
@Override
public void delete(Resource resource) throws PersistenceException {
}
@Override
public Resource create(Resource resource, String s, Map<String, Object> map) throws PersistenceException {
Resource childResource = resource.getChild(s);
if (childResource != null) {
throw new IllegalArgumentException("Child " + s + " already exists ");
}
String resourceType = (String) map.get("sling:resourceType");
if (resourceType == null) {
resourceType = (String) map.get("jcr:primaryType");
}
if (resourceType == null) {
LOG.warn("Resource type null for {} {} ", resource, resource.getPath() + "/" + s);
}
Resource newResource = new MockRealResource(this, resource.getPath() + "/" + s, resourceType, map);
repository.put(newResource.getPath(), newResource);
LOG.debug("Created Resource {} ", newResource.getPath());
return newResource;
}
@Override
public void revert() {
}
@Override
public void commit() throws PersistenceException {
LOG.debug("Committing");
for (Map.Entry<String, Resource> e : repository.entrySet()) {
LOG.debug("Committing {} ", e.getKey());
Resource r = e.getValue();
ModifiableValueMap vm = r.adaptTo(ModifiableValueMap.class);
for (Map.Entry<String, Object> me : vm.entrySet()) {
if (me.getValue() instanceof InputStream) {
try {
String value = IOUtils.toString((InputStream) me.getValue());
LOG.debug("Converted {} {} ", me.getKey(), value);
vm.put(me.getKey(), value);
} catch (IOException e1) {
throw new PersistenceException("Failed to commit input stream", e1);
}
}
}
LOG.debug("Converted {} ", vm);
}
LOG.debug("Comittted {} ", repository);
}
@Override
public boolean hasChanges() {
return false;
}
};
SlingHttpServletRequest request = new MockSlingHttpServlet3Request(null, null, null, null, null) {
@Override
public Object getAttribute(String name) {
if ("request-parts-iterator".equals(name)) {
return partsIterator;
}
return super.getAttribute(name);
}
@Override
public ResourceResolver getResourceResolver() {
return resourceResolver;
}
};
streamedUplodOperation.doRun(request, response, changes);
{
Resource r = repository.get("/test/upload/location/test1.txt");
Assert.assertNotNull(r);
ValueMap m = r.adaptTo(ValueMap.class);
Assert.assertNotNull(m);
Assert.assertEquals("nt:file", m.get("jcr:primaryType"));
}
{
Resource r = repository.get("/test/upload/location/test1.txt/jcr:content");
Assert.assertNotNull(r);
ValueMap m = r.adaptTo(ValueMap.class);
Assert.assertNotNull(m);
Assert.assertEquals("nt:resource", m.get("jcr:primaryType"));
Assert.assertTrue(m.get("jcr:lastModified") instanceof Calendar);
Assert.assertEquals("text/plain", m.get("jcr:mimeType"));
Assert.assertEquals("testpart", m.get("jcr:data"));
}
{
Resource r = repository.get("/test/upload/location/test2.txt");
Assert.assertNotNull(r);
ValueMap m = r.adaptTo(ValueMap.class);
Assert.assertNotNull(m);
Assert.assertEquals("nt:file", m.get("jcr:primaryType"));
}
{
Resource r = repository.get("/test/upload/location/test2.txt/jcr:content");
Assert.assertNotNull(r);
ValueMap m = r.adaptTo(ValueMap.class);
Assert.assertNotNull(m);
Assert.assertEquals("nt:resource", m.get("jcr:primaryType"));
Assert.assertTrue(m.get("jcr:lastModified") instanceof Calendar);
Assert.assertEquals("text/plain2", m.get("jcr:mimeType"));
Assert.assertEquals("test1234", m.get("jcr:data"));
}
}
use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class DefaultErrorHandlerServlet method service.
@Override
public void service(ServletRequest req, ServletResponse res) throws IOException {
// get settings
Integer scObject = (Integer) req.getAttribute(SlingConstants.ERROR_STATUS);
String statusMessage = (String) req.getAttribute(SlingConstants.ERROR_MESSAGE);
String requestUri = (String) req.getAttribute(SlingConstants.ERROR_REQUEST_URI);
String servletName = (String) req.getAttribute(SlingConstants.ERROR_SERVLET_NAME);
// ensure values
int statusCode = (scObject != null) ? scObject.intValue() : HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
if (statusMessage == null) {
statusMessage = statusToString(statusCode);
}
// start the response message
final PrintWriter pw = sendIntro((HttpServletResponse) res, statusCode, statusMessage, requestUri, servletName);
// write the exception message
final PrintWriter escapingWriter = new PrintWriter(ResponseUtil.getXmlEscapingWriter(pw));
// dump the stack trace
if (req.getAttribute(SlingConstants.ERROR_EXCEPTION) instanceof Throwable) {
final Throwable throwable = (Throwable) req.getAttribute(SlingConstants.ERROR_EXCEPTION);
pw.println("<h3>Exception:</h3>");
pw.println("<pre>");
pw.flush();
printStackTrace(escapingWriter, throwable);
escapingWriter.flush();
pw.println("</pre>");
}
// dump the request progress tracker
if (req instanceof SlingHttpServletRequest) {
final RequestProgressTracker tracker = ((SlingHttpServletRequest) req).getRequestProgressTracker();
pw.println("<h3>Request Progress:</h3>");
pw.println("<pre>");
pw.flush();
tracker.dump(escapingWriter);
escapingWriter.flush();
pw.println("</pre>");
}
// conclude the response message
sendEpilogue(pw);
}
use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class StreamingUploadOperationTest method testParts.
@Test
public void testParts() throws PersistenceException, RepositoryException, UnsupportedEncodingException {
List<Modification> changes = new ArrayList<>();
PostResponse response = new AbstractPostResponse() {
@Override
protected void doSend(HttpServletResponse response) throws IOException {
}
@Override
public void onChange(String type, String... arguments) {
}
@Override
public String getPath() {
return "/test/upload/location";
}
};
List<Part> partsList = new ArrayList<>();
partsList.add(new MockPart("test1.txt@Length", null, null, 0, new ByteArrayInputStream("8".getBytes("UTF-8")), Collections.EMPTY_MAP));
partsList.add(new MockPart("test1.txt@Offset", null, null, 0, new ByteArrayInputStream("0".getBytes("UTF-8")), Collections.EMPTY_MAP));
partsList.add(new MockPart("test1.txt", "text/plain", "test1bad.txt", 4, new ByteArrayInputStream("test".getBytes("UTF-8")), mapOf("Content-Length", "4")));
partsList.add(new MockPart("test1.txt@Offset", null, null, 0, new ByteArrayInputStream("4".getBytes("UTF-8")), Collections.EMPTY_MAP));
partsList.add(new MockPart("test1.txt", "text/plain", "test1bad.txt", 4, new ByteArrayInputStream("part".getBytes("UTF-8")), mapOf("Content-Length", "4")));
partsList.add(new MockPart("*", "text/plain2", "test2.txt", 8, new ByteArrayInputStream("test1234".getBytes("UTF-8")), Collections.EMPTY_MAP));
partsList.add(new MockPart("badformfield2", null, null, 0, new ByteArrayInputStream("testbadformfield2".getBytes("UTF-8")), Collections.EMPTY_MAP));
final Iterator<Part> partsIterator = partsList.iterator();
final Map<String, Resource> repository = new HashMap<>();
final ResourceResolver resourceResolver = new MockResourceResolver() {
@Override
public Resource getResource(String path) {
Resource resource = repository.get(path);
if (resource == null) {
if ("/test/upload/location".equals(path)) {
resource = new MockRealResource(this, path, "sling:Folder");
repository.put(path, resource);
LOG.debug("Created {} ", path);
}
}
LOG.debug("Resource {} is {} {}", path, resource, ResourceUtil.isSyntheticResource(resource));
return resource;
}
@Override
public Iterable<Resource> getChildren(Resource resource) {
List<Resource> children = new ArrayList<>();
for (Map.Entry<String, Resource> e : repository.entrySet()) {
if (isChild(resource.getPath(), e.getKey())) {
children.add(e.getValue());
}
}
return children;
}
private boolean isChild(String path, String key) {
if (key.length() > path.length() && key.startsWith(path)) {
return !key.substring(path.length() + 1).contains("/");
}
return false;
}
@Override
public Iterator<Resource> listChildren(Resource parent) {
return getChildren(parent).iterator();
}
@Override
public void delete(Resource resource) throws PersistenceException {
}
@Override
public Resource create(Resource resource, String s, Map<String, Object> map) throws PersistenceException {
Resource childResource = resource.getChild(s);
if (childResource != null) {
throw new IllegalArgumentException("Child " + s + " already exists ");
}
String resourceType = (String) map.get("sling:resourceType");
if (resourceType == null) {
resourceType = (String) map.get("jcr:primaryType");
}
if (resourceType == null) {
LOG.warn("Resource type null for {} {} ", resource, resource.getPath() + "/" + s);
}
Resource newResource = new MockRealResource(this, resource.getPath() + "/" + s, resourceType, map);
repository.put(newResource.getPath(), newResource);
LOG.debug("Created Resource {} ", newResource.getPath());
return newResource;
}
@Override
public void revert() {
}
@Override
public void commit() throws PersistenceException {
LOG.debug("Committing");
for (Map.Entry<String, Resource> e : repository.entrySet()) {
LOG.debug("Committing {} ", e.getKey());
Resource r = e.getValue();
ModifiableValueMap vm = r.adaptTo(ModifiableValueMap.class);
for (Map.Entry<String, Object> me : vm.entrySet()) {
if (me.getValue() instanceof InputStream) {
try {
String value = IOUtils.toString((InputStream) me.getValue());
LOG.debug("Converted {} {} ", me.getKey(), value);
vm.put(me.getKey(), value);
} catch (IOException e1) {
throw new PersistenceException("Failed to commit input stream", e1);
}
}
}
LOG.debug("Converted {} ", vm);
}
LOG.debug("Comittted {} ", repository);
}
@Override
public boolean hasChanges() {
return false;
}
};
SlingHttpServletRequest request = new MockSlingHttpServlet3Request(null, null, null, null, null) {
@Override
public Object getAttribute(String name) {
if ("request-parts-iterator".equals(name)) {
return partsIterator;
}
return super.getAttribute(name);
}
@Override
public ResourceResolver getResourceResolver() {
return resourceResolver;
}
};
streamedUplodOperation.doRun(request, response, changes);
{
Resource r = repository.get("/test/upload/location/test1.txt");
Assert.assertNotNull(r);
ValueMap m = r.adaptTo(ValueMap.class);
Assert.assertNotNull(m);
Assert.assertEquals("nt:file", m.get("jcr:primaryType"));
}
{
Resource r = repository.get("/test/upload/location/test1.txt/jcr:content");
Assert.assertNotNull(r);
ValueMap m = r.adaptTo(ValueMap.class);
Assert.assertNotNull(m);
Assert.assertEquals("nt:resource", m.get("jcr:primaryType"));
Assert.assertTrue(m.get("jcr:lastModified") instanceof Calendar);
Assert.assertEquals("text/plain", m.get("jcr:mimeType"));
Assert.assertEquals("testpart", m.get("jcr:data"));
}
{
Resource r = repository.get("/test/upload/location/test2.txt");
Assert.assertNotNull(r);
ValueMap m = r.adaptTo(ValueMap.class);
Assert.assertNotNull(m);
Assert.assertEquals("nt:file", m.get("jcr:primaryType"));
}
{
Resource r = repository.get("/test/upload/location/test2.txt/jcr:content");
Assert.assertNotNull(r);
ValueMap m = r.adaptTo(ValueMap.class);
Assert.assertNotNull(m);
Assert.assertEquals("nt:resource", m.get("jcr:primaryType"));
Assert.assertTrue(m.get("jcr:lastModified") instanceof Calendar);
Assert.assertEquals("text/plain2", m.get("jcr:mimeType"));
Assert.assertEquals("test1234", m.get("jcr:data"));
}
}
use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class SlingPostServletTest method testRedirection.
private void testRedirection(String requestPath, String resourcePath, String redirect, String expected) throws Exception {
RedirectServletResponse resp = new RedirectServletResponse();
SlingHttpServletRequest request = new RedirectServletRequest(redirect, requestPath);
PostResponse htmlResponse = new HtmlResponse();
htmlResponse.setPath(resourcePath);
assertEquals(expected != null, servlet.redirectIfNeeded(request, htmlResponse, resp));
assertEquals(expected, resp.redirectLocation);
}
Aggregations