001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/presence/trunk/presence-api/api/src/java/org/sakaiproject/presence/api/PresenceService.java $
003: * $Id: PresenceService.java 7844 2006-04-17 13:06:02Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 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.roster;
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: import org.sakaiproject.util.ResourceLoader;
038:
039: public class ParticipantImageServlet extends HttpServlet {
040: private ResourceLoader msgs = new ResourceLoader(
041: "org.sakaiproject.tool.roster.bundle.Messages");
042: private static final Log LOG = LogFactory
043: .getLog(ParticipantImageServlet.class);
044: private static final String UNIVERSITY_ID_PHOTO = "photo";
045: private static final String CONTENT_TYPE = "image/jpeg";
046: private ProfileManager profileManager;
047:
048: /**
049: * The doGet method of the servlet. <br>
050: *
051: * This method is called when a form has its tag value method equals to get.
052: *
053: * @param request the request send by the client to the server
054: * @param response the response send by the server to the client
055: * @throws ServletException if an error occurred
056: * @throws IOException if an error occurred
057: */
058: public void doGet(HttpServletRequest request,
059: HttpServletResponse response) throws ServletException,
060: IOException {
061: if (LOG.isDebugEnabled())
062: LOG.debug("doGet(HttpServletRequest" + request
063: + ", HttpServletResponse" + response + ")");
064: response.setContentType(CONTENT_TYPE);
065: String userId = null;
066: OutputStream stream = response.getOutputStream();
067: userId = (String) request.getParameter(UNIVERSITY_ID_PHOTO);
068: if (userId != null && userId.trim().length() > 0) {
069: displayUniversityIDPhoto(userId, stream, response);
070: } else {
071: displayLocalImage(stream);
072: }
073:
074: }
075:
076: private void displayUniversityIDPhoto(String userId,
077: OutputStream stream, HttpServletResponse response) {
078: if (LOG.isDebugEnabled())
079: LOG
080: .debug("displayUniversityIDPhoto (String "
081: + userId
082: + "OutputStream stream, HttpServletResponse response)");
083: try {
084: byte[] displayPhoto;
085: displayPhoto = getProfileManager()
086: .getInstitutionalPhotoByUserId(userId, true);
087: if (displayPhoto != null && displayPhoto.length > 0) {
088: LOG.debug("Display University ID photo for user:"
089: + userId);
090: response.setContentLength(displayPhoto.length);
091: stream.write(displayPhoto);
092: stream.flush();
093:
094: } else {
095: LOG.debug("Display University ID photo for user:"
096: + userId + " is unavailable");
097: displayLocalImage(stream);
098: }
099: } catch (IOException e) {
100: LOG.error(e.getMessage(), e);
101: }
102:
103: }
104:
105: private void displayLocalImage(OutputStream stream) {
106: String unavailable_image = msgs.getString("img_off_unavail");
107:
108: if (LOG.isDebugEnabled())
109: LOG.debug("displayPhotoUnavailable(OutputStream" + stream
110: + ", String " + unavailable_image + ")");
111: try {
112: BufferedInputStream in = null;
113: try {
114:
115: in = new BufferedInputStream(new FileInputStream(
116: getServletContext().getRealPath(
117: unavailable_image)));
118: int ch;
119:
120: while ((ch = in.read()) != -1) {
121: stream.write((char) ch);
122: }
123: }
124:
125: finally {
126: if (in != null)
127: in.close();
128: }
129: } catch (FileNotFoundException e) {
130: LOG.error(e.getMessage(), e);
131: } catch (IOException e) {
132: LOG.error(e.getMessage(), e);
133: }
134: }
135:
136: /**
137: * get the component manager
138: * @return profile manager
139: */
140: public ProfileManager getProfileManager() {
141: LOG.debug("getProfileManager()");
142: if (profileManager == null) {
143: return (ProfileManager) ComponentManager
144: .get(ProfileManager.class.getName());
145: }
146: return profileManager;
147: }
148:
149: }
|