1 17 18 package org.pentaho.ui.servlet; 19 20 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 25 import javax.servlet.ServletException ; 26 import javax.servlet.ServletOutputStream ; 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpServletResponse ; 29 import org.jfree.io.IOUtils; 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.pentaho.core.repository.IContentItem; 33 import org.pentaho.core.repository.IContentRepository; 34 import org.pentaho.core.session.IPentahoSession; 35 import org.pentaho.core.system.PentahoSystem; 36 import org.pentaho.messages.Messages; 37 import org.pentaho.messages.util.LocaleHelper; 38 39 public class GetContent extends ServletBase { 40 41 44 private static final long serialVersionUID = 9019152264205996418L; 45 46 private static final Log logger = LogFactory.getLog(GetContent.class); 47 48 public Log getLogger() { 49 return logger; 50 } 51 52 public GetContent() { 53 super(); 54 } 55 56 protected void setupSession(IPentahoSession userSession) { 57 PentahoSystem.sessionStartup(userSession); 58 } 59 60 protected void removeUserSession(IPentahoSession userSession) { 61 userSession.destroy(); 62 } 63 64 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 65 doGet(request, response); 66 } 67 68 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 69 70 response.setCharacterEncoding(LocaleHelper.getSystemEncoding()); 71 72 PentahoSystem.systemEntryPoint(); 73 try { 74 IPentahoSession userSession = getPentahoSession(request); 75 76 String id = request.getParameter("id"); if (id == null) { 78 returnError(response, Messages.getErrorString("GetContent.ERROR_0001_ID_PARAMETER_EMPTY")); return; 80 } 81 82 IContentRepository contentRepos = PentahoSystem.getContentRepository(userSession); 83 if (contentRepos == null) { 84 returnError(response, Messages.getString("GetContent.ERROR_0002_CONTENT_REPOS_UNAVAILABLE")); return; 86 } 87 88 try { 89 IContentItem contentItem = contentRepos.getContentItemById(id); 90 if (contentItem == null) { 91 returnError(response, Messages.getString("GetContent.ERROR_0005_CONTENT_NOT_FOUND", id)); return; 93 } 94 95 String mimetype = contentItem.getMimeType(); 96 if ((mimetype == null) || (mimetype.length() < 1)) { 97 mimetype = request.getParameter("mimetype"); } 99 100 if ((mimetype != null) && (mimetype.length() > 0)) { 102 response.setContentType(mimetype); 103 } 104 105 InputStream inStr = contentItem.getInputStream(); 107 ServletOutputStream outStr = response.getOutputStream(); 108 109 110 try { 111 IOUtils.getInstance().copyStreams(inStr, outStr); 112 } finally { 113 inStr.close(); 114 outStr.close(); 115 } 116 } catch (Throwable t) { 117 error(Messages.getString("GetContent.ERROR_0003_CONTENT_READ_ERROR")); } 119 120 } finally { 121 PentahoSystem.systemExitPoint(); 122 } 123 } 124 125 void returnError(HttpServletResponse response, String message) { 126 error(message); 127 128 response.setContentType("text/plain"); try { 130 response.getWriter().println(Messages.getString("GetContent.ERROR_0004_RETURN_MESSAGE") + message); } catch (Throwable t) { 132 } 133 } 134 135 } 136 | Popular Tags |