001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.lenya.cms.publication;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.io.OutputStream;
023:
024: import org.apache.avalon.framework.logger.AbstractLogEnabled;
025: import org.apache.avalon.framework.logger.Logger;
026: import org.apache.cocoon.servlet.multipart.Part;
027: import org.apache.commons.io.IOUtils;
028: import org.apache.commons.io.output.ByteArrayOutputStream;
029:
030: /**
031: * Wrapper class for the ODT resource type.
032: */
033: public class OpenDocumentWrapper extends AbstractLogEnabled {
034:
035: /**
036: * The extension for ODT documents.
037: */
038: public static final String ODT_EXTENSION = "odt";
039:
040: /**
041: * The mime-type for ODT documents.
042: */
043: public static final String ODT_MIME_TYPE = "application/vnd.oasis.opendocument.text";
044:
045: private Document delegate;
046:
047: /**
048: * @param doc The document to wrap.
049: * @param logger The logger.
050: */
051: public OpenDocumentWrapper(Document doc, Logger logger) {
052: enableLogging(logger);
053: this .delegate = doc;
054: }
055:
056: /**
057: * @param file The part to write.
058: */
059: public void write(Part file) {
060: if (!file.getMimeType().equals(ODT_MIME_TYPE)) {
061: throw new IllegalArgumentException("Invalid mime type: ["
062: + file.getMimeType() + "]");
063: }
064: try {
065: write(file.getInputStream());
066: } catch (IOException e) {
067: throw new RuntimeException(e);
068: }
069: }
070:
071: /**
072: * Writes the content of the resource.
073: * @param inputStream The input stream providing the content.
074: */
075: public void write(InputStream inputStream) {
076: final ByteArrayOutputStream sourceBos = new ByteArrayOutputStream();
077:
078: OutputStream destOutputStream = null;
079: try {
080:
081: IOUtils.copy(inputStream, sourceBos);
082:
083: destOutputStream = delegate.getOutputStream();
084: IOUtils.write(sourceBos.toByteArray(), destOutputStream);
085:
086: delegate.setMimeType(ODT_MIME_TYPE);
087:
088: } catch (Exception e) {
089: throw new RuntimeException(e);
090: } finally {
091: if (destOutputStream != null) {
092: try {
093: destOutputStream.flush();
094: destOutputStream.close();
095: } catch (Exception ignore) {
096: }
097: }
098: }
099:
100: }
101:
102: }
|