001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/trunk/component/src/java/org/sakaiproject/tool/assessment/qti/helper/AuthoringHelper.java $
003: * $Id: AuthoringHelper.java 9274 2006-05-10 22:50:48Z daisyf@stanford.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2007 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.assessment.qti.helper;
021:
022: import java.io.BufferedInputStream;
023: import java.io.ByteArrayOutputStream;
024: import java.io.File;
025: import java.io.FileInputStream;
026: import java.io.FileNotFoundException;
027: import java.io.IOException;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.sakaiproject.content.api.ContentResource;
032: import org.sakaiproject.content.cover.ContentHostingService;
033: import org.sakaiproject.entity.api.ResourcePropertiesEdit;
034: import org.sakaiproject.exception.IdInvalidException;
035: import org.sakaiproject.exception.IdUsedException;
036: import org.sakaiproject.exception.InconsistentException;
037: import org.sakaiproject.exception.OverQuotaException;
038: import org.sakaiproject.exception.PermissionException;
039: import org.sakaiproject.exception.ServerOverloadException;
040: import org.sakaiproject.tool.cover.ToolManager;
041: import org.sakaiproject.user.api.User;
042: import org.sakaiproject.user.cover.UserDirectoryService;
043: import org.sakaiproject.event.cover.NotificationService;
044:
045: public class AttachmentHelper {
046: private static Log log = LogFactory.getLog(AttachmentHelper.class);
047:
048: public ContentResource createContentResource(String fullFilePath,
049: String filename, String mimeType) {
050: ContentResource contentResource = null;
051: int BUFFER_SIZE = 2048;
052: byte tempContent[] = new byte[BUFFER_SIZE];
053: File file = null;
054: FileInputStream fileInputStream = null;
055: BufferedInputStream bufInputStream = null;
056: ByteArrayOutputStream byteArrayOutputStream = null;
057: byte content[];
058: int count = 0;
059:
060: try {
061: if (mimeType.equalsIgnoreCase("text/url")) {
062: content = filename.getBytes();
063: } else {
064: file = new File(fullFilePath);
065: fileInputStream = new FileInputStream(file);
066: bufInputStream = new BufferedInputStream(
067: fileInputStream);
068: byteArrayOutputStream = new ByteArrayOutputStream();
069: while ((count = bufInputStream.read(tempContent, 0,
070: BUFFER_SIZE)) != -1) {
071: byteArrayOutputStream.write(tempContent, 0, count);
072: }
073: content = byteArrayOutputStream.toByteArray();
074: }
075:
076: ResourcePropertiesEdit props = ContentHostingService
077: .newResourceProperties();
078: // Maybe we need to put in some properties?
079: // props.addProperty(ResourceProperties.PROP_DISPLAY_NAME, name);
080: // props.addProperty(ResourceProperties.PROP_DESCRIPTION, name);
081:
082: contentResource = ContentHostingService
083: .addAttachmentResource(filename, ToolManager
084: .getCurrentPlacement().getContext(),
085: ToolManager.getTool("sakai.samigo")
086: .getTitle(), mimeType, content,
087: props);
088: } catch (IdInvalidException e) {
089: log.error("IdInvalidException:" + e.getMessage());
090: } catch (PermissionException e) {
091: log.error("PermissionException:" + e.getMessage());
092: } catch (InconsistentException e) {
093: log.error("InconsistentException:" + e.getMessage());
094: } catch (IdUsedException e) {
095: log.error("IdUsedException:" + e.getMessage());
096: } catch (OverQuotaException e) {
097: log.error("OverQuotaException:" + e.getMessage());
098: } catch (ServerOverloadException e) {
099: log.error("ServerOverloadException:" + e.getMessage());
100: } catch (FileNotFoundException e) {
101: log.error("FileNotFoundException:" + e.getMessage());
102: } catch (IOException e) {
103: log.error("IOException:" + e.getMessage());
104: } finally {
105: if (bufInputStream != null) {
106: try {
107: bufInputStream.close();
108: } catch (IOException e) {
109: log.error(e.getMessage());
110: }
111: }
112: if (fileInputStream != null) {
113: try {
114: fileInputStream.close();
115: } catch (IOException e) {
116: log.error(e.getMessage());
117: }
118: }
119: if (byteArrayOutputStream != null) {
120: try {
121: byteArrayOutputStream.close();
122: } catch (IOException e) {
123: log.error(e.getMessage());
124: }
125: }
126: }
127:
128: return contentResource;
129: }
130: }
|