001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/profile/tags/sakai_2-4-1/profile-app/src/java/org/sakaiproject/tool/profile/ProfileImageServlet.java $
003: * $Id: ProfileImageServlet.java 8424 2006-04-27 20:23:44Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.tool.profile;
021:
022: import java.io.BufferedInputStream;
023: import java.io.FileInputStream;
024: import java.io.FileNotFoundException;
025: import java.io.IOException;
026: import java.io.OutputStream;
027:
028: import javax.servlet.ServletException;
029: import javax.servlet.http.HttpServlet;
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpServletResponse;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035: import org.sakaiproject.api.app.profile.ProfileManager;
036: import org.sakaiproject.component.cover.ComponentManager;
037:
038: public class ProfileImageServlet extends HttpServlet {
039: private static final String PHOTO = "photo";
040:
041: private static final String CONTENT_TYPE = "image/jpeg";
042:
043: private static final String IMAGE_PATH = "/images/";
044:
045: private static final String UNAVAILABLE_IMAGE = "/officialPhotoUnavailable.jpg";
046:
047: private ProfileManager profileManager;
048:
049: private static final Log LOG = LogFactory
050: .getLog(ProfileImageServlet.class);
051:
052: /**
053: * The doGet method of the servlet. <br>
054: * This method is called when a form has its tag value method equals to get.
055: *
056: * @param request
057: * the request send by the client to the server
058: * @param response
059: * the response send by the server to the client
060: * @throws ServletException
061: * if an error occurred
062: * @throws IOException
063: * if an error occurred
064: */
065: public void doGet(HttpServletRequest request,
066: HttpServletResponse response) throws ServletException,
067: IOException {
068: if (LOG.isDebugEnabled())
069: LOG.debug("doGet(HttpServletRequest" + request
070: + ", HttpServletResponse" + response + ")");
071: response.setContentType(CONTENT_TYPE);
072: String userId = null;
073: byte[] institutionalPhoto;
074: userId = (String) request.getParameter(PHOTO);
075: if (userId != null && userId.trim().length() > 0) {
076: institutionalPhoto = getProfileManager()
077: .getInstitutionalPhotoByUserId(userId);
078: OutputStream stream = response.getOutputStream();
079: if (institutionalPhoto != null
080: && institutionalPhoto.length > 0) {
081: LOG.debug("Display University ID photo for user:"
082: + userId);
083: response.setContentLength(institutionalPhoto.length);
084: stream.write(institutionalPhoto);
085: stream.flush();
086: } else {
087: try {
088: BufferedInputStream in = null;
089: try {
090:
091: in = new BufferedInputStream(
092: new FileInputStream(getServletContext()
093: .getRealPath(IMAGE_PATH)
094: + UNAVAILABLE_IMAGE));
095: int ch;
096:
097: while ((ch = in.read()) != -1) {
098: LOG
099: .debug("Display University ID photo for user:"
100: + userId
101: + " is unavailable");
102: stream.write((char) ch);
103: }
104: }
105:
106: finally {
107: if (in != null)
108: in.close();
109: }
110: } catch (FileNotFoundException e) {
111: LOG.error(e.getMessage(), e);
112: } catch (IOException e) {
113: LOG.error(e.getMessage(), e);
114: }
115: }
116: }
117: }
118:
119: /**
120: * get the component manager
121: *
122: * @return profile manager
123: */
124: public ProfileManager getProfileManager() {
125: if (profileManager == null) {
126: return (ProfileManager) ComponentManager
127: .get(ProfileManager.class.getName());
128: }
129: return profileManager;
130: }
131:
132: }
|