001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.content;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.io.OutputStream;
023: import java.util.Map;
024:
025: import de.finix.contelligent.CallData;
026: import de.finix.contelligent.Component;
027: import de.finix.contelligent.category.CategoryCombinationNotSupportedException;
028: import de.finix.contelligent.core.TimeService;
029: import de.finix.contelligent.exception.ComponentPersistenceException;
030: import de.finix.contelligent.exception.ContelligentException;
031: import de.finix.contelligent.logging.LoggingService;
032: import de.finix.contelligent.resource.BinaryResource;
033:
034: /**
035: * This class represents binary content that is managed by contelligent
036: */
037: public class ContelligentBinaryContent extends ContelligentContent
038: implements BinaryContent {
039:
040: final static org.apache.log4j.Logger log = LoggingService
041: .getLogger(ContelligentBinaryContent.class);
042:
043: public ContelligentBinaryContent(Component component)
044: throws Exception {
045: super (component);
046: }
047:
048: public Object getContentValue(CallData callData)
049: throws IOException, ComponentPersistenceException,
050: CategoryCombinationNotSupportedException {
051: return getBinaryResource(callData);
052: }
053:
054: public long lastModified(CallData callData) throws IOException,
055: ComponentPersistenceException,
056: CategoryCombinationNotSupportedException {
057: return getBinaryResource(callData).getLastModified();
058: }
059:
060: public void streamBinary(OutputStream outputStream,
061: CallData callData) throws IOException {
062: try {
063: getBinaryResource(callData).stream(outputStream);
064: } catch (ComponentPersistenceException e) {
065: throw new IOException("Could not stream binary resource: "
066: + e);
067: } catch (CategoryCombinationNotSupportedException e) {
068: e.printStackTrace(); // To change body of catch statement use
069: // Options | File Templates.
070: }
071: }
072:
073: public String getContentType(CallData callData)
074: throws ComponentPersistenceException,
075: CategoryCombinationNotSupportedException {
076: return getBinaryResource(callData).getContentType();
077: }
078:
079: public String getExtension(CallData callData)
080: throws ComponentPersistenceException,
081: CategoryCombinationNotSupportedException {
082: return getBinaryResource(callData).getExtension();
083: }
084:
085: public int length(CallData callData)
086: throws CategoryCombinationNotSupportedException,
087: ComponentPersistenceException {
088: return getBinaryResource(callData).length();
089: }
090:
091: /**
092: * Create a binary resource and set the resource into the Content of the
093: * binary component.
094: */
095:
096: public void setBinary(InputStream stream, String contentType,
097: String extension, long size, CallData callData)
098: throws ContelligentException {
099:
100: BinaryResource resource = new BinaryResource(stream,
101: contentType, extension, TimeService.getInstance()
102: .currentTimeMillis(), (int) size);
103: setResource(resource, callData);
104: }
105:
106: private BinaryResource getBinaryResource(CallData callData)
107: throws ComponentPersistenceException,
108: CategoryCombinationNotSupportedException {
109: Map categoryMap = callData.getCategoryMap();
110: String identifier = getCategoryManager()
111: .createUniqueCategoryIdentifier(
112: getSensitiveCategories(), categoryMap);
113: BinaryResource binaryResource = (BinaryResource) getResource(identifier);
114:
115: if (binaryResource == null) {
116: identifier = getCategoryManager()
117: .createUniqueCategoryIdentifier(
118: getSensitiveCategories());
119: }
120: if (binaryResource == null) {
121: log.debug("The requested binary for category combination ["
122: + identifier + "] is not supported!");
123: throw new CategoryCombinationNotSupportedException(
124: "The requested binary for category combination ["
125: + identifier + "] is not supported!");
126: }
127: return binaryResource;
128: }
129:
130: }
|