001/* 002 * ModeShape (http://www.modeshape.org) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.modeshape.web; 017 018import java.io.IOException; 019import java.io.InputStream; 020import java.io.PrintWriter; 021import java.util.logging.Logger; 022import javax.jcr.Binary; 023import javax.jcr.Node; 024import javax.jcr.PathNotFoundException; 025import javax.jcr.Property; 026import javax.jcr.Session; 027import javax.servlet.ServletException; 028import javax.servlet.http.HttpServlet; 029import javax.servlet.http.HttpServletRequest; 030import javax.servlet.http.HttpServletResponse; 031import org.modeshape.web.server.Connector; 032 033/** 034 * 035 * @author kulikov 036 */ 037public class BinaryContentServlet extends HttpServlet { 038 039 private static final long serialVersionUID = 1L; 040 private static Logger log = Logger.getLogger("Binary"); 041 042 /** 043 * Processes requests for both HTTP 044 * <code>GET</code> and 045 * <code>POST</code> methods. 046 * 047 * @param request servlet request 048 * @param response servlet response 049 * @throws ServletException if a servlet-specific error occurs 050 * @throws IOException if an I/O error occurs 051 */ 052 protected void processRequest(HttpServletRequest request, HttpServletResponse response) 053 throws ServletException, IOException { 054 log.severe("Binary content has been requested"); 055 056 String repository = request.getParameter("repository"); 057 String workspace = request.getParameter("workspace"); 058 String path = request.getParameter("path"); 059 String property = request.getParameter("property"); 060 061 Connector connector = (Connector) request.getSession().getAttribute("connector"); 062 try { 063 Session session = connector.find(repository).session(workspace); 064 Node node = session.getNode(path); 065 066 Property p; 067 try { 068 p = node.getProperty(property); 069 } catch (PathNotFoundException e) { 070 response.setContentType("text/html"); 071 PrintWriter writer = response.getWriter(); 072 writer.write("<html>"); 073 writer.write("<p>Content not found or recently removed</p>"); 074 writer.write("</html>"); 075 return; 076 } 077 078 Binary binary = p.getBinary(); 079 080 081 InputStream in = binary.getStream(); 082 String contentType = node.getProperty("jcr:mimeType").getString(); 083 084 response.setContentType(contentType); 085 086 int b; 087 while ((b = in.read()) != -1) { 088 response.getOutputStream().write(b); 089 } 090 091 log.severe("Sent binary content"); 092 binary.dispose(); 093 094 } catch (Exception e) { 095 throw new ServletException(e); 096 } 097 } 098 099 // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> 100 /** 101 * Handles the HTTP 102 * <code>GET</code> method. 103 * 104 * @param request servlet request 105 * @param response servlet response 106 * @throws ServletException if a servlet-specific error occurs 107 * @throws IOException if an I/O error occurs 108 */ 109 @Override 110 protected void doGet(HttpServletRequest request, HttpServletResponse response) 111 throws ServletException, IOException { 112 processRequest(request, response); 113 } 114 115 /** 116 * Handles the HTTP 117 * <code>POST</code> method. 118 * 119 * @param request servlet request 120 * @param response servlet response 121 * @throws ServletException if a servlet-specific error occurs 122 * @throws IOException if an I/O error occurs 123 */ 124 @Override 125 protected void doPost(HttpServletRequest request, HttpServletResponse response) 126 throws ServletException, IOException { 127 processRequest(request, response); 128 } 129 130 /** 131 * Returns a short description of the servlet. 132 * 133 * @return a String containing servlet description 134 */ 135 @Override 136 public String getServletInfo() { 137 return "Short description"; 138 }// </editor-fold> 139}