Created
September 9, 2016 15:20
-
-
Save paul-vargas/d693b9a09645db1e6b9d000a6100f4ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.paulvargas.blog.rs; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.util.AbstractMap.SimpleEntry; | |
import java.util.List; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import javax.ejb.EJB; | |
import javax.ejb.Stateless; | |
import javax.ejb.TransactionAttribute; | |
import javax.ejb.TransactionAttributeType; | |
import javax.servlet.ServletContext; | |
import javax.servlet.ServletException; | |
import javax.servlet.ServletOutputStream; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import javax.ws.rs.Consumes; | |
import javax.ws.rs.FormParam; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.POST; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.PathParam; | |
import javax.ws.rs.core.Context; | |
import javax.ws.rs.core.MediaType; | |
import org.omnifaces.filter.CacheControlFilter; | |
import org.paulvargas.blog.ejb.ArchiveEJB; | |
import org.paulvargas.blog.ejb.CommentEJB; | |
import org.paulvargas.blog.ejb.PostEJB; | |
import org.paulvargas.blog.model.Archive; | |
import org.paulvargas.blog.model.Comment; | |
import org.paulvargas.blog.model.Post; | |
import org.paulvargas.tools.util.ClassLoaderUtil; | |
@Path("blog") | |
@Stateless | |
public class BlogResource { | |
private static final Logger LOG = Logger.getLogger(BlogResource.class.getName()); | |
private static void copy(InputStream source, OutputStream target) throws IOException { | |
byte[] buffer = new byte[8192]; | |
for (int n; (n = source.read(buffer)) > 0;) { | |
target.write(buffer, 0, n); | |
} | |
} | |
@EJB | |
private PostEJB postEJB; | |
@EJB | |
private CommentEJB commentEJB; | |
@EJB | |
private ArchiveEJB archiveEJB; | |
@GET | |
public void getHome(@Context HttpServletRequest request, @Context HttpServletResponse response, @Context ServletContext context) { | |
try { | |
request.setAttribute("posts", postEJB.getPosts()); | |
request.getRequestDispatcher("/WEB-INF/pages/blog/home.jsp").forward(request, response); | |
} catch (ServletException | IOException e) { | |
LOG.log(Level.SEVERE, "There was an error getting the posts.", e); | |
} | |
} | |
@GET | |
@Path("post/{id : \\d+}") | |
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) | |
public void getPost(@Context HttpServletRequest request, @Context HttpServletResponse response, @Context ServletContext context, @PathParam("id") int id) { | |
try { | |
SimpleEntry<Post, List<Comment>> entry = postEJB.getPost(id); | |
Post post = entry.getKey(); | |
String content = post.getContent(); | |
content = content.replace("blog/image/", "../../blog/image/"); | |
content = content.replace("blog/pdf/", "../../blog/pdf/"); | |
post.setContent(content); | |
request.setAttribute("post", entry); | |
request.getRequestDispatcher("/WEB-INF/pages/blog/post.jsp").forward(request, response); | |
} catch (ServletException | IOException e) { | |
LOG.log(Level.SEVERE, "There was an error getting the post.", e); | |
} | |
} | |
@POST | |
@Path("post/{id : \\d+}") | |
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) | |
public void postComment(@Context HttpServletRequest request, @Context HttpServletResponse response, @Context ServletContext context, @PathParam("id") int id, @FormParam("parentId") int parentId, @FormParam("level") int level, @FormParam("name") String name, @FormParam("email") String email, @FormParam("title") String title, @FormParam("content") String content) { | |
try { | |
content = content.replaceAll("\r?\n", "<br>"); | |
commentEJB.postComment(parentId, level, name, email, title, content); | |
request.setAttribute("post", postEJB.getPost(id)); | |
request.getRequestDispatcher("/WEB-INF/pages/blog/post.jsp").forward(request, response); | |
} catch (ServletException | IOException e) { | |
LOG.log(Level.SEVERE, "There was an error posting the comment.", e); | |
} | |
} | |
@GET | |
@Path("image/{id : \\d+}") | |
public void getImage(@Context HttpServletRequest request, @Context HttpServletResponse response, @Context ServletContext context, @PathParam("id") int id) { | |
CacheControlFilter.setCacheHeaders(response, 900); // 15 min | |
try { | |
Archive archive = archiveEJB.findArchive(id); | |
if (archive != null) { | |
byte[] data = archive.getData(); | |
response.setContentType("image/png"); | |
response.setContentLength(data.length); | |
ServletOutputStream outputStream = response.getOutputStream(); | |
outputStream.write(data); | |
outputStream.flush(); | |
outputStream.close(); | |
} | |
} catch (IOException e) { | |
LOG.log(Level.SEVERE, "There was an error getting the image.", e); | |
} | |
} | |
@GET | |
@Path("pdf/{id : \\d+}") | |
public void getPdf(@Context HttpServletRequest request, @Context HttpServletResponse response, @Context ServletContext context, @PathParam("id") int id) { | |
CacheControlFilter.setCacheHeaders(response, 900); // 15 min | |
try { | |
Archive archive = archiveEJB.findArchive(id); | |
if (archive != null) { | |
byte[] data = archive.getData(); | |
response.setContentType("application/pdf"); | |
response.setContentLength(data.length); | |
ServletOutputStream outputStream = response.getOutputStream(); | |
outputStream.write(data); | |
outputStream.flush(); | |
outputStream.close(); | |
} | |
} catch (IOException e) { | |
LOG.log(Level.SEVERE, "There was an error getting the pdf.", e); | |
} | |
} | |
@GET | |
@Path("css/{filename : .*}") | |
public void getStylesheet(@Context HttpServletRequest request, @Context HttpServletResponse response, @Context ServletContext context, @PathParam("filename") String filename) { | |
CacheControlFilter.setCacheHeaders(response, 31104000); // 1 year | |
try { | |
InputStream source = ClassLoaderUtil.getResourceAsStream("org/paulvargas/blog/resources/" + filename, this.getClass()); | |
if (source != null) { | |
response.setContentType("text/css"); | |
ServletOutputStream target = response.getOutputStream(); | |
copy(source, target); | |
} else { | |
response.sendError(HttpServletResponse.SC_NOT_FOUND); | |
} | |
} catch (IOException e) { | |
LOG.log(Level.SEVERE, "There was an error getting the stylesheet.", e); | |
} | |
} | |
@GET | |
@Path("fonts/{filename : .*}") | |
public void getFont(@Context HttpServletRequest request, @Context HttpServletResponse response, @Context ServletContext context, @PathParam("filename") String filename) { | |
CacheControlFilter.setCacheHeaders(response, 31104000); // 1 year | |
try { | |
InputStream source = ClassLoaderUtil.getResourceAsStream("org/paulvargas/blog/resources/" + filename, this.getClass()); | |
if (source != null) { | |
ServletOutputStream target = response.getOutputStream(); | |
copy(source, target); | |
} else { | |
response.sendError(HttpServletResponse.SC_NOT_FOUND); | |
} | |
} catch (IOException e) { | |
LOG.log(Level.SEVERE, "There was an error getting the font.", e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment