001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.core.cms.command;
023:
024: import org.jboss.mx.util.MBeanProxy;
025: import org.jboss.mx.util.MBeanServerLocator;
026: import org.jboss.portal.cms.CMS;
027: import org.jboss.portal.cms.Command;
028: import org.jboss.portal.cms.model.Content;
029: import org.jboss.portal.cms.model.File;
030: import org.jboss.portal.common.invocation.InvocationException;
031: import org.jboss.portal.core.controller.ControllerCommand;
032: import org.jboss.portal.core.controller.ControllerException;
033: import org.jboss.portal.core.controller.ControllerResponse;
034: import org.jboss.portal.core.controller.command.info.CommandInfo;
035: import org.jboss.portal.core.controller.command.info.ViewCommandInfo;
036: import org.jboss.portal.core.controller.command.response.StreamContentResponse;
037: import org.jboss.portal.server.ServerInvocation;
038:
039: import javax.management.MBeanServer;
040: import javax.management.ObjectName;
041: import java.io.InputStream;
042: import java.util.Locale;
043:
044: /**
045: * Renders content from the cms.
046: *
047: * @author <a href="mailto:roy@jboss.org">Roy Russo</a>
048: */
049: public class StreamContentCommand extends ControllerCommand {
050:
051: private static final CommandInfo info = new ViewCommandInfo();
052:
053: private String path;
054:
055: public StreamContentCommand(String path)
056: throws IllegalArgumentException {
057: this .path = path;
058: }
059:
060: public String getPath() {
061: return path;
062: }
063:
064: public CommandInfo getInfo() {
065: return info;
066: }
067:
068: public ControllerResponse execute() throws InvocationException,
069: ControllerException {
070: try {
071: MBeanServer mbeanServer = MBeanServerLocator.locateJBoss();
072: CMS CMSService = (CMS) MBeanProxy.get(CMS.class,
073: new ObjectName("portal:service=CMS"), mbeanServer);
074: ServerInvocation serverInvocation = getControllerContext()
075: .getServerInvocation();
076: Command getCMD = CMSService.getCommandFactory()
077: .createFileGetCommand(path,
078: serverInvocation.getRequest().getLocale());
079: File file = (File) CMSService.execute(getCMD);
080: if (file == null) {
081: getCMD = CMSService.getCommandFactory()
082: .createFileGetCommand(
083: path,
084: new Locale(CMSService
085: .getDefaultLocale()));
086: file = (File) CMSService.execute(getCMD);
087: }
088: Content content = file.getContent();
089: String mimeType = content.getMimeType();
090: long lastModified;
091: if (content.getLastModified() != null) {
092: lastModified = content.getLastModified().getTime();
093: } else {
094: // Use current time in none available
095: lastModified = System.currentTimeMillis();
096: }
097: InputStream inputStream = content.getStream();
098: return new StreamContentResponse(mimeType, lastModified,
099: inputStream);
100: } catch (Exception e) {
101: // try
102: // {
103: // resp.sendError(HttpServletResponse.SC_NOT_FOUND, e.getMessage());
104: // }
105: // catch(IOException e1)
106: // {
107: // log.error("An error occured when wanting to stream the command", e);
108: // }
109: throw new ControllerException(e);
110: }
111: }
112: }
|