001: /* Copyright 2001, 2005 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.channels;
007:
008: import java.io.IOException;
009: import java.io.OutputStream;
010: import java.util.Enumeration;
011: import java.util.HashSet;
012: import java.util.Iterator;
013: import java.util.Map;
014: import java.util.Set;
015: import java.util.TreeSet;
016:
017: import org.jasig.portal.IMimeResponse;
018: import org.jasig.portal.PortalException;
019: import org.jasig.portal.UPFileSpec;
020: import org.jasig.portal.security.IPerson;
021: import org.jasig.portal.services.PersonDirectory;
022: import org.jasig.portal.services.persondir.IPersonAttributeDao;
023: import org.jasig.portal.utils.DocumentFactory;
024: import org.jasig.portal.utils.XSLT;
025: import org.w3c.dom.Document;
026: import org.w3c.dom.Element;
027: import org.xml.sax.ContentHandler;
028:
029: /**
030: * This channel demonstrates the method of obtaining and displaying
031: * standard uPortal person attributes.
032: *
033: * Implements IMimeResponse in order to support the inline display of jpegPhotos
034: * Note: for proper operation, one should use an idempotent baseActionURL.
035: *
036: * @author Ken Weiner, kweiner@unicon.net
037: * @author Yuji Shinozaki, ys2n@virginia.edu
038: * @version $Revision: 36388 $ $Date: 2006-01-06 10:59:42 -0700 (Fri, 06 Jan 2006) $
039: */
040: public class CPersonAttributes extends BaseChannel implements
041: IMimeResponse {
042:
043: private static final String sslLocation = "CPersonAttributes/CPersonAttributes.ssl";
044:
045: public void renderXML(ContentHandler out) throws PortalException {
046: IPerson person = staticData.getPerson();
047: Document doc = DocumentFactory.getNewDocument();
048:
049: Element attributesE = doc.createElement("attributes");
050:
051: IPersonAttributeDao pa = PersonDirectory
052: .getPersonAttributeDao();
053: Set possibleAttrs = pa.getPossibleUserAttributeNames();
054:
055: if (possibleAttrs != null)
056: possibleAttrs = new HashSet(possibleAttrs);
057: else
058: possibleAttrs = new HashSet();
059:
060: for (Enumeration attribs = person.getAttributeNames(); attribs
061: .hasMoreElements();) {
062: // Get the attribute name
063: String attName = (String) attribs.nextElement();
064:
065: // Remove this attr from the list of possible attrs
066: possibleAttrs.remove(attName);
067:
068: // Set the attribute
069: Element attributeE = doc.createElement("attribute");
070:
071: Element nameE = doc.createElement("name");
072: nameE.appendChild(doc.createTextNode(attName));
073: attributeE.appendChild(nameE);
074:
075: // Get the IPerson attribute value for this eduPerson attribute name
076: if (person.getAttributeValues(attName) != null) {
077: Object[] values = person.getAttributeValues(attName);
078: for (int i = 0; i < values.length; i++) {
079: if (log.isTraceEnabled())
080: log.trace("type of value[" + i + "] is "
081: + values[i].getClass().getName());
082: String value = values[i].toString();
083: Element valueE = doc.createElement("value");
084: valueE.appendChild(doc.createTextNode(value));
085: attributeE.appendChild(valueE);
086: }
087: }
088:
089: attributesE.appendChild(attributeE);
090: }
091:
092: //Sort the set of possible attributes
093: possibleAttrs = new TreeSet(possibleAttrs);
094:
095: //Add the unknown attributes to the element list.
096: for (Iterator attribs = possibleAttrs.iterator(); attribs
097: .hasNext();) {
098: // Get the attribute name
099: String attName = (String) attribs.next();
100:
101: // Set the attribute
102: Element attributeE = doc.createElement("attribute");
103:
104: Element nameE = doc.createElement("name");
105: nameE.appendChild(doc.createTextNode(attName));
106: attributeE.appendChild(nameE);
107:
108: attributesE.appendChild(attributeE);
109: }
110:
111: doc.appendChild(attributesE);
112:
113: XSLT xslt = XSLT.getTransformer(this , runtimeData.getLocales());
114: xslt.setXML(doc);
115: xslt.setStylesheetParameter("baseActionURL", runtimeData
116: .getBaseActionURL());
117: xslt.setStylesheetParameter("downloadWorkerURL",
118: runtimeData.getBaseWorkerURL(
119: UPFileSpec.FILE_DOWNLOAD_WORKER, true));
120: xslt.setXSL(sslLocation, runtimeData.getBrowserInfo());
121: xslt.setTarget(out);
122: xslt.transform();
123: }
124:
125: // IMimeResponse implementation -- ys2n@virginia.edu
126: /**
127: * Returns the MIME type of the content.
128: */
129: public java.lang.String getContentType() {
130: // In the future we will need some sort of way of grokking the
131: // mime-type of the byte-array and returning an appropriate mime-type
132: // Right now there is no good way of doing that, and we will
133: // assume that the only thing we will be delivering is a jpegPhoto.
134: // attribute with a mimetype of image/jpeg
135: // In the future however, we may need a way to deliver different
136: // attributes as differenct mimetypes (e.g certs).
137: //
138: String mimetype;
139: // runtime parameter "attribute" determines which attribute to return when
140: // called as an IMimeResponse.
141: String attrName = runtimeData.getParameter("attribute");
142:
143: if ("jpegPhoto".equals(attrName)) {
144: mimetype = "image/jpeg";
145: } else {
146: // default -- an appropriate choice?
147: mimetype = "application/octet-stream";
148: }
149: return mimetype;
150: }
151:
152: /**
153: * Returns the MIME content in the form of an input stream.
154: * Returns null if the code needs the OutputStream object
155: */
156: public java.io.InputStream getInputStream() throws IOException {
157: String attrName = runtimeData.getParameter("attribute");
158: IPerson person = staticData.getPerson();
159:
160: if (attrName == null) {
161: attrName = "";
162: }
163:
164: // get the image out of the IPerson as a byte array.
165: // Note: I am assuming here that the only thing that this
166: // IMimeResponse will return is a jpegPhoto. Some other
167: // generalized mechanism will need to be inserted here to
168: // support other mimetypes and IPerson attributes.
169: byte[] imgBytes = (byte[]) person.getAttribute(attrName);
170:
171: // need to create a ByteArrayInputStream()
172:
173: if (imgBytes == null) {
174: imgBytes = new byte[0]; // let's avoid a null pointer
175: }
176: java.io.InputStream is = (java.io.InputStream) new java.io.ByteArrayInputStream(
177: imgBytes);
178:
179: return is;
180: }
181:
182: /**
183: * Pass the OutputStream object to the download code if it needs special handling
184: * (like outputting a Zip file). Unimplemented.
185: */
186: public void downloadData(OutputStream out) throws IOException {
187: }
188:
189: /**
190: * Returns the name of the MIME file.
191: */
192: public java.lang.String getName() {
193: // As noted above the only attribute we support right now is "image/jpeg" for
194: // the jpegPhoto attribute.
195:
196: String payloadName;
197: if ("jpegPhoto".equals(runtimeData.getParameter("attribute")))
198: payloadName = "image.jpg";
199: else
200: payloadName = "unknown";
201: return payloadName;
202: }
203:
204: /**
205: * Returns a list of header values that can be set in the HttpResponse.
206: * Returns null if no headers need to be set.
207: */
208: public Map getHeaders() {
209: return null;
210: }
211:
212: /**
213: * Let the channel know that there were problems with the download
214: * @param e
215: */
216: public void reportDownloadError(Exception e) {
217: log.error(e.getMessage(), e);
218: }
219:
220: }
|