Skip to content

Instantly share code, notes, and snippets.

@ugocei
Created March 11, 2015 10:07
Show Gist options
  • Save ugocei/d028bcdef7a47b0e861c to your computer and use it in GitHub Desktop.
Save ugocei/d028bcdef7a47b0e861c to your computer and use it in GitHub Desktop.
How to avoid SlingRepository.loginAdministrative
<%@page import="org.apache.sling.api.request.RequestParameterMap, org.apache.sling.jcr.api.SlingRepository, javax.jcr.Node, org.apache.jackrabbit.commons.JcrUtils"%>
<%@include file="/libs/foundation/global.jsp"%>
<%
// if we have a "to" parameter...
if (slingRequest.getRequestParameter("to") != null) {
String TEMP_PATH = "/tmp/mails/";
SlingRepository repository = sling.getService(SlingRepository.class);
Session adminSession = repository.loginAdministrative(null);
RequestParameterMap params = slingRequest.getRequestParameterMap();
// create a node at TEMP_PATH+"to"...
Node mailNode = JcrUtils.getOrCreateByPath(
TEMP_PATH + params.getValue("to").getString(),
true,
"nt:unstructured",
"nt:unstructured",
adminSession,
true);
// and store message properties for later delivery..
for (String key : params.keySet()) {
mailNode.setProperty(key, params.getValue(key).getString());
}
adminSession.save();
// logout admin session for security reasons!
adminSession.logout();
%>Thank you for the nice email! :)<%
}
else {
%>
<form method=GET action=/content/geometrixx-consulting/mailform.html>
Subject: <br><input type=text name=subject><br>
To: <br><input type=text name=to><br>
Message: <br>
<textarea name=message cols=80 rows=25>
</textarea><br>
<input type=submit>
</form>
<% } %>
<%@page%>
<%@include file="/libs/foundation/global.jsp"%>
<form method=POST action=/content/geometrixx-consulting/mailform.mailpost.html>
Subject: <br><input type=text name=subject><br>
To: <br><input type=text name=to><br>
Message: <br>
<textarea name=message cols=80 rows=25>
</textarea><br>
<input type=submit>
</form>
package com.geometrixx.consulting.website.components.mailform;
public interface MailFormProcessor {
void processMailRequest(String to, String subject, String message) throws Exception;
}
package com.geometrixx.consulting.website.components.mailform.impl;
import java.util.HashMap;
import java.util.Map;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import com.geometrixx.consulting.website.components.mailform.MailFormProcessor;
@Component
@Service
public class MailFormProcessorImpl implements MailFormProcessor {
private static final String TEMP_PATH = "/tmp/mails";
@Reference
private ResourceResolverFactory resolverFactory;
@Override
public void processMailRequest(String path, String subject, String message) throws Exception {
Map<String, Object> authenticationInfo = new HashMap<String, Object>();
ResourceResolver resolver = resolverFactory.getServiceResourceResolver(authenticationInfo);
try {
Resource parent = resolver.getResource(TEMP_PATH);
if (parent == null) {
throw new RuntimeException("Can't access " + TEMP_PATH);
}
Map<String, Object> props = new HashMap<String, Object>();
props.put("jcr:title", subject);
props.put("message", message);
props.put("jcr:primaryType", "nt:unstructured");
resolver.create(parent, path, props);
resolver.commit();
} finally {
resolver.close();
}
}
}
package com.geometrixx.consulting.website.components.mailform;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
@SlingServlet(resourceTypes = "sling/servlet/default",
selectors = "mailpost",
extensions = "html",
methods = { "POST" })
public class MailFormServlet extends SlingAllMethodsServlet {
@Reference
private MailFormProcessor mailFormProcessor;
@Override
protected void doPost(SlingHttpServletRequest request,
SlingHttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try {
// We should verify that this is a valid email address
String to = request.getParameter("to");
String subject = request.getParameter("subject");
String message = request.getParameter("message");
mailFormProcessor.processMailRequest(to, subject, message);
out.println("<html><body><p>OK</p></body></html>");
} catch (Exception e) {
throw new ServletException(e.getMessage(), e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment