Author: phuong_vu
Date: 2010-08-09 23:24:42 -0400 (Mon, 09 Aug 2010)
New Revision: 3784
Added:
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/web/command/
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/web/command/Command.java
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/web/command/CommandHandler.java
exo/portal/branches/3.1.x/component/web/src/test/java/org/exoplatform/web/command/
exo/portal/branches/3.1.x/component/web/src/test/java/org/exoplatform/web/command/TestCommandHandler.java
exo/portal/branches/3.1.x/webui/eXo/src/main/java/org/exoplatform/web/command/handler/GetApplicationHandler.java
exo/portal/branches/3.1.x/webui/eXo/src/main/java/org/exoplatform/web/command/handler/HelloJCRHandler.java
Modified:
exo/portal/branches/3.1.x/component/web/src/main/java/conf/portal/configuration.xml
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/download/DownloadService.java
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/upload/UploadService.java
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/upload/UploadSessionListener.java
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/web/WebAppController.java
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/web/WebRequestHandler.java
exo/portal/branches/3.1.x/examples/portal/war/src/main/webapp/WEB-INF/web.xml
exo/portal/branches/3.1.x/testsuite/webuibasedsamples/src/main/java/org/exoplatform/sample/webui/component/UISampleDownloadUpload.java
exo/portal/branches/3.1.x/web/eXoResources/src/main/webapp/javascript/eXo/webui/UIUpload.js
exo/portal/branches/3.1.x/web/portal/src/main/webapp/WEB-INF/web.xml
exo/portal/branches/3.1.x/webui/core/src/main/java/org/exoplatform/webui/form/UIFormUploadInput.java
exo/portal/branches/3.1.x/webui/eXo/src/main/java/org/exoplatform/web/command/handler/DownloadHandler.java
exo/portal/branches/3.1.x/webui/eXo/src/main/java/org/exoplatform/web/command/handler/UploadHandler.java
Log:
revert : not use commons-fileupload yet, fix CommandHandler.java, just execute Upload or
Download Command type
Modified:
exo/portal/branches/3.1.x/component/web/src/main/java/conf/portal/configuration.xml
===================================================================
---
exo/portal/branches/3.1.x/component/web/src/main/java/conf/portal/configuration.xml 2010-08-10
03:03:07 UTC (rev 3783)
+++
exo/portal/branches/3.1.x/component/web/src/main/java/conf/portal/configuration.xml 2010-08-10
03:24:42 UTC (rev 3784)
@@ -63,30 +63,4 @@
<type>org.exoplatform.web.filter.ExtensibleFilter</type>
</component>
- <external-component-plugins>
-
<target-component>org.exoplatform.services.listener.ListenerService</target-component>
- <component-plugin>
-
<name>org.exoplatform.web.GenericHttpListener.sessionDestroyed</name>
- <set-method>addListener</set-method>
- <type>org.exoplatform.upload.UploadSessionListener</type>
- </component-plugin>
- </external-component-plugins>
-
- <external-component-plugins>
-
<target-component>org.exoplatform.web.WebAppController</target-component>
- <component-plugin>
- <name>UploadHandler</name>
- <set-method>register</set-method>
- <type>org.exoplatform.web.command.handler.UploadHandler</type>
- </component-plugin>
- </external-component-plugins>
-
- <external-component-plugins>
-
<target-component>org.exoplatform.web.WebAppController</target-component>
- <component-plugin>
- <name>DownloadHandler</name>
- <set-method>register</set-method>
- <type>org.exoplatform.web.command.handler.DownloadHandler</type>
- </component-plugin>
- </external-component-plugins>
</configuration>
Modified:
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/download/DownloadService.java
===================================================================
---
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/download/DownloadService.java 2010-08-10
03:03:07 UTC (rev 3783)
+++
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/download/DownloadService.java 2010-08-10
03:24:42 UTC (rev 3784)
@@ -41,8 +41,6 @@
private PortalContainerInfo pinfo_;
- public static final String DOWNLOAD_HANDLER_PATH = "download";
-
public DownloadService(PortalContainerInfo pinfo, InitParams params) throws Exception
{
int maxSize =
Integer.parseInt(params.getValueParam("download.resource.cache.size").getValue());
@@ -84,8 +82,8 @@
public String getDownloadLink(String id)
{
- return "/" + pinfo_.getContainerName() + "/" +
DOWNLOAD_HANDLER_PATH + "?"
- + "resourceId=" + id;
+ return "/" + pinfo_.getContainerName() + "/command?"
+ +
"type=org.exoplatform.web.command.handler.DownloadHandler&resourceId=" +
id;
}
@SuppressWarnings("serial")
@@ -99,6 +97,7 @@
maxSize_ = maxSize;
}
+ @SuppressWarnings("unused")
protected boolean removeEldestEntry(Map.Entry<String, DownloadResource>
eldest)
{
return size() > maxSize_;
Modified:
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/upload/UploadService.java
===================================================================
---
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/upload/UploadService.java 2010-08-10
03:03:07 UTC (rev 3783)
+++
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/upload/UploadService.java 2010-08-10
03:24:42 UTC (rev 3784)
@@ -19,29 +19,29 @@
package org.exoplatform.upload;
-import org.apache.commons.fileupload.FileItem;
-import org.apache.commons.fileupload.FileUploadException;
-import org.apache.commons.fileupload.ProgressListener;
-import org.apache.commons.fileupload.disk.DiskFileItem;
-import org.apache.commons.fileupload.disk.DiskFileItemFactory;
-import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.exoplatform.container.xml.InitParams;
import org.exoplatform.container.xml.PortalContainerInfo;
import org.gatein.common.logging.Logger;
import org.gatein.common.logging.LoggerFactory;
import java.io.File;
-import java.util.HashSet;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
import java.util.LinkedHashMap;
-import java.util.List;
import java.util.Map;
-import java.util.Set;
import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpSession;
+/**
+ * Created by The eXo Platform SARL
+ * Author : Tuan Nguyen
+ * tuan.nguyen(a)exoplatform.com
+ * Dec 8, 2006
+ */
public class UploadService
{
+
/** . */
private static final Logger log = LoggerFactory.getLogger(UploadService.class);
@@ -53,8 +53,6 @@
private Map<String, Integer> uploadLimitsMB_ = new LinkedHashMap<String,
Integer>();
- public static String UPLOAD_RESOURCES_STACK = "uploadResourcesStack";
-
public UploadService(PortalContainerInfo pinfo, InitParams params) throws Exception
{
String tmpDir = System.getProperty("java.io.tmpdir");
@@ -68,142 +66,127 @@
uploadDir.mkdirs();
}
- /**
- * Create UploadResource for HttpServletRequest
- *
- * @param requestow
- * the webapp's {@link javax.servlet.http.HttpServletRequest}
- * @throws FileUploadException
- */
- @SuppressWarnings("unchecked")
- public void createUploadResource(HttpServletRequest request) throws
FileUploadException
+ public void createUploadResource(HttpServletRequest request) throws IOException
{
String uploadId = request.getParameter("uploadId");
- UploadResource upResource = new UploadResource(uploadId);
- upResource.setFileName("");// Avoid NPE in UploadHandler
- uploadResources.put(upResource.getUploadId(), upResource);
-
- putToStackInSession(request.getSession(true), uploadId);
+ // by default, use the limit set in the service
+ //int limitMB = defaultUploadLimitMB_;
+ // if the limit is set in the request (specific for this upload) then use this
value instead of the default one
+ //if (uploadLimitsMB_.containsKey(uploadId)) limitMB =
uploadLimitsMB_.get(uploadId).intValue() ;
+ int limitMB = uploadLimitsMB_.get(uploadId).intValue();
- double contentLength = request.getContentLength();
- upResource.setEstimatedSize(contentLength);
- if (isLimited(upResource, contentLength))
- {
+ UploadResource upResource = new UploadResource(uploadId);
+ RequestStreamReader reader = new RequestStreamReader(upResource);
+ int estimatedSizeMB = (request.getContentLength() / 1024) / 1024;
+ if (limitMB > 0 && estimatedSizeMB > limitMB)
+ { // a limit set to 0 means unlimited
upResource.setStatus(UploadResource.FAILED_STATUS);
+ //upResource.setLimitMB(limitMB);
+ uploadResources.put(uploadId, upResource);
+ log.debug("Upload cancelled because file bigger than size limit : " +
estimatedSizeMB + " MB > "
+ + limitMB + " MB");
+ // WebuiRequestContext ctx = WebuiRequestContext.getCurrentInstance();
+ // UIApplication uiApp = ctx.getUIApplication();
+ // uiApp.addMessage(new ApplicationMessage("The file must be <
"+limitMB+" MB.", null, ApplicationMessage.WARNING));
return;
}
+ // TODO : display error message, terminate upload correctly
- ServletFileUpload servletFileUpload = makeServletFileUpload(upResource);
- // parse request
- List<FileItem> itemList = servletFileUpload.parseRequest(request);
- if (itemList == null || itemList.size() != 1 || itemList.get(0).isFormField())
+ String headerEncoding = request.getCharacterEncoding();
+ Map<String, String> headers = reader.parseHeaders(request.getInputStream(),
headerEncoding);
+
+ String fileName = reader.getFileName(headers);
+ if (fileName == null)
+ fileName = uploadId;
+ fileName = fileName.substring(fileName.lastIndexOf('\\') + 1);
+
+ upResource.setFileName(fileName);
+ upResource.setMimeType(headers.get(RequestStreamReader.CONTENT_TYPE));
+ upResource.setStoreLocation(uploadLocation_ + "/" + uploadId +
"." + fileName);
+ upResource.setEstimatedSize(request.getContentLength());
+
+ uploadResources.put(upResource.getUploadId(), upResource);
+
+ File fileStore = new File(upResource.getStoreLocation());
+ if (!fileStore.exists())
+ fileStore.createNewFile();
+ FileOutputStream output = new FileOutputStream(fileStore);
+ reader.readBodyData(request, output);
+
+ if (upResource.getStatus() == UploadResource.UPLOADING_STATUS)
{
- log.debug("Please upload 1 file per request");
+ upResource.setStatus(UploadResource.UPLOADED_STATUS);
return;
}
- DiskFileItem fileItem = (DiskFileItem)itemList.get(0);
- String fileName = fileItem.getName();
- String storeLocation = uploadLocation_ + "/" + uploadId + "." +
fileName;
+ uploadResources.remove(uploadId);
+ fileStore.delete();
+ }
- // commons-fileupload will store the temp file with name *.tmp
- // we need to rename it to our desired name
- fileItem.getStoreLocation().renameTo(new File(storeLocation));
+ public void createUploadResource(String uploadId, String encoding, String contentType,
double contentLength,
+ InputStream inputStream) throws Exception
+ {
+ UploadResource upResource = new UploadResource(uploadId);
+ RequestStreamReader reader = new RequestStreamReader(upResource);
+ int limitMB = uploadLimitsMB_.get(uploadId).intValue();
+ int estimatedSizeMB = (int)contentLength / 1024 / 1024;
+ if (limitMB > 0 && estimatedSizeMB > limitMB)
+ { // a limit set to 0 means unlimited
+ upResource.setStatus(UploadResource.FAILED_STATUS);
+ uploadResources.put(uploadId, upResource);
+ log.debug("Upload cancelled because file bigger than size limit : " +
estimatedSizeMB + " MB > "
+ + limitMB + " MB");
+ return;
+ }
+ Map<String, String> headers = reader.parseHeaders(inputStream, encoding);
+ String fileName = reader.getFileName(headers);
+ if (fileName == null)
+ fileName = uploadId;
+ fileName = fileName.substring(fileName.lastIndexOf('\\') + 1);
+
upResource.setFileName(fileName);
- upResource.setMimeType(fileItem.getContentType());
- upResource.setStoreLocation(storeLocation);
- upResource.setStatus(UploadResource.UPLOADED_STATUS);
- }
+ upResource.setMimeType(headers.get(RequestStreamReader.CONTENT_TYPE));
+ upResource.setStoreLocation(uploadLocation_ + "/" + uploadId +
"." + fileName);
+ upResource.setEstimatedSize(contentLength);
+ uploadResources.put(upResource.getUploadId(), upResource);
+ File fileStore = new File(upResource.getStoreLocation());
+ if (!fileStore.exists())
+ fileStore.createNewFile();
+ FileOutputStream output = new FileOutputStream(fileStore);
+ reader.readBodyData(inputStream, contentType, output);
- @SuppressWarnings("unchecked")
- private void putToStackInSession(HttpSession session, String uploadId)
- {
- Set<String> uploadResouceIds =
(Set<String>)session.getAttribute(UploadService.UPLOAD_RESOURCES_STACK);
- if (uploadResouceIds == null)
+ if (upResource.getStatus() == UploadResource.UPLOADING_STATUS)
{
- uploadResouceIds = new HashSet();
+ upResource.setStatus(UploadResource.UPLOADED_STATUS);
+ return;
}
- uploadResouceIds.add(uploadId);
- session.setAttribute(UploadService.UPLOAD_RESOURCES_STACK, uploadResouceIds);
+
+ uploadResources.remove(uploadId);
+ fileStore.delete();
}
- /**
- * Get UploadResource by uploadId
- *
- * @param uploadId
- * uploadId of UploadResource
- * @return org.exoplatform.upload.UploadResource of uploadId
- */
public UploadResource getUploadResource(String uploadId)
- {
- return uploadResources.get(uploadId);
+ {//throws Exception
+ UploadResource upResource = uploadResources.get(uploadId);
+ return upResource;
}
- /**
- * Clean up temporary files that are uploaded in the Session but not removed yet
- *
- * @param session
- */
- @SuppressWarnings("unchecked")
- public void cleanUp(HttpSession session)
- {
- log.debug("Cleaning up uploaded files for temporariness");
- Set<String> uploadIds =
(Set<String>)session.getAttribute(UploadService.UPLOAD_RESOURCES_STACK);
- if (uploadIds != null)
- {
- for (String id : uploadIds)
- {
- removeUploadResource(id);
- uploadLimitsMB_.remove(id);
- }
- }
- }
-
- /**
- * @deprecated use {@link #removeUploadResource(String)} instead
- *
- * @param uploadId
- */
- @Deprecated
public void removeUpload(String uploadId)
{
- removeUploadResource(uploadId);
- }
-
- /**
- * Remove the UploadResource and its temporary file that associated with given
<code>uploadId</code>.
- * <br/>If <code>uploadId</code> is null or UploadResource is null,
do nothing
- *
- * @param uploadId uploadId of UploadResource will be removed
- */
- public void removeUploadResource(String uploadId)
- {
if (uploadId == null)
return;
UploadResource upResource = uploadResources.get(uploadId);
- if (upResource != null)
- {
- uploadResources.remove(uploadId);
-
- if (upResource.getStoreLocation() != null)
- {
- File file = new File(upResource.getStoreLocation());
- file.delete();
- }
- }
-
- // uploadLimitsMB_.remove(uploadId);
+ if (upResource == null)
+ return;
+ if (upResource.getStoreLocation() == null)
+ return;
+ File file = new File(upResource.getStoreLocation());
+ file.delete();
+ uploadResources.remove(uploadId);
+ //uploadLimitsMB_.remove(uploadId);
}
- /**
- * Registry upload limit size for uploadLimitsMB_. If limitMB is null,
- * defaultUploadLimitMB_ will be registried
- *
- * @param uploadId
- * @param limitMB
- * upload limit size
- */
public void addUploadLimit(String uploadId, Integer limitMB)
{
if (limitMB == null)
@@ -212,61 +195,8 @@
uploadLimitsMB_.put(uploadId, limitMB);
}
- /**
- * Get all upload limit sizes
- *
- * @return all upload limit sizes
- */
public Map<String, Integer> getUploadLimitsMB()
{
return uploadLimitsMB_;
}
-
- private ServletFileUpload makeServletFileUpload(final UploadResource upResource)
- {
- // Create a factory for disk-based file items
- DiskFileItemFactory factory = new DiskFileItemFactory();
-
- // Set factory constraints
- factory.setSizeThreshold(0);
- factory.setRepository(new File(uploadLocation_));
-
- // Create a new file upload handler
- ServletFileUpload upload = new ServletFileUpload(factory);
- ProgressListener listener = new ProgressListener()
- {
- public void update(long pBytesRead, long pContentLength, int pItems)
- {
- if (pBytesRead == upResource.getUploadedSize())
- return;
- upResource.addUploadedBytes(pBytesRead - upResource.getUploadedSize());
- }
- };
- upload.setProgressListener(listener);
- return upload;
- }
-
- private boolean isLimited(UploadResource upResource, double contentLength)
- {
- // by default, use the limit set in the service
- int limitMB = defaultUploadLimitMB_;
- // if the limit is set in the request (specific for this upload) then use
- // this value instead of the default one
- if (uploadLimitsMB_.containsKey(upResource.getUploadId()))
- {
- limitMB = uploadLimitsMB_.get(upResource.getUploadId()).intValue();
- }
-
- int estimatedSizeMB = (int)((contentLength / 1024) / 1024);
- if (limitMB > 0 && estimatedSizeMB > limitMB)
- { // a limit set to 0 means unlimited
- if (log.isDebugEnabled())
- {
- log.debug("Upload cancelled because file bigger than size limit : "
+ estimatedSizeMB + " MB > " + limitMB
- + " MB");
- }
- return true;
- }
- return false;
- }
}
\ No newline at end of file
Modified:
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/upload/UploadSessionListener.java
===================================================================
---
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/upload/UploadSessionListener.java 2010-08-10
03:03:07 UTC (rev 3783)
+++
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/upload/UploadSessionListener.java 2010-08-10
03:24:42 UTC (rev 3784)
@@ -1,30 +0,0 @@
-package org.exoplatform.upload;
-
-import javax.servlet.http.HttpSession;
-import javax.servlet.http.HttpSessionEvent;
-
-import org.exoplatform.container.PortalContainer;
-import org.exoplatform.services.listener.Event;
-import org.exoplatform.services.listener.Listener;
-
-/**
- * This listener for the purpose of cleaning up temporary files that are uploaded to the
server
- * but not removed by specific actions from user
- *
- * The listener is triggered when a session is destroyed
- *
- * @author <a href="mailto:trongtt@gmail.com">Tran The Trong</a>
- * @version $Revision$
- */
-public class UploadSessionListener extends Listener<PortalContainer,
HttpSessionEvent>
-{
- @Override
- public void onEvent(Event<PortalContainer, HttpSessionEvent> event) throws
Exception
- {
- PortalContainer container = event.getSource();
- HttpSession session = event.getData().getSession();
-
- UploadService uploadService =
(UploadService)container.getComponentInstanceOfType(UploadService.class);
- uploadService.cleanUp(session);
- }
-}
Modified:
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/web/WebAppController.java
===================================================================
---
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/web/WebAppController.java 2010-08-10
03:03:07 UTC (rev 3783)
+++
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/web/WebAppController.java 2010-08-10
03:24:42 UTC (rev 3784)
@@ -19,6 +19,15 @@
package org.exoplatform.web;
+import org.exoplatform.container.ExoContainer;
+import org.exoplatform.container.ExoContainerContext;
+import org.exoplatform.container.component.ComponentRequestLifecycle;
+import org.exoplatform.container.component.RequestLifeCycle;
+import org.exoplatform.services.log.ExoLogger;
+import org.exoplatform.services.log.Log;
+import org.exoplatform.web.application.Application;
+import org.exoplatform.web.command.CommandHandler;
+
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -26,13 +35,6 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import org.exoplatform.container.ExoContainer;
-import org.exoplatform.container.ExoContainerContext;
-import org.exoplatform.container.component.RequestLifeCycle;
-import org.exoplatform.services.log.ExoLogger;
-import org.exoplatform.services.log.Log;
-import org.exoplatform.web.application.Application;
-
/**
* Created by The eXo Platform SAS
* Mar 21, 2007
@@ -65,8 +67,10 @@
applications_ = new HashMap<String, Application>();
attributes_ = new HashMap<String, Object>();
handlers_ = new HashMap<String, WebRequestHandler>();
+ register(new CommandHandler());
}
+ @SuppressWarnings("unused")
public Object getAttribute(String name, Object value)
{
return attributes_.get(name);
Modified:
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/web/WebRequestHandler.java
===================================================================
---
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/web/WebRequestHandler.java 2010-08-10
03:03:07 UTC (rev 3783)
+++
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/web/WebRequestHandler.java 2010-08-10
03:24:42 UTC (rev 3784)
@@ -22,8 +22,6 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import org.exoplatform.container.component.BaseComponentPlugin;
-
/**
* Created by The eXo Platform SAS
* Mar 21, 2007
@@ -33,9 +31,9 @@
* In case of portal the path is /portal but you could return your own from the getPath()
method and hence the
* WebAppController would use your own handler
*
- * The execute method is to be overridden and the buisness logic should be handled here
+ * The execute method is to be overideen and the buisness logic should be handled here
*/
-abstract public class WebRequestHandler extends BaseComponentPlugin
+abstract public class WebRequestHandler
{
public void onInit(WebAppController controller) throws Exception
Added:
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/web/command/Command.java
===================================================================
---
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/web/command/Command.java
(rev 0)
+++
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/web/command/Command.java 2010-08-10
03:24:42 UTC (rev 3784)
@@ -0,0 +1,43 @@
+/**
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.web.command;
+
+import org.exoplatform.web.WebAppController;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * Created by The eXo Platform SARL
+ * Author : Tuan Nguyen
+ * tuan.nguyen(a)exoplatform.com
+ * May 22, 2007
+ */
+abstract public class Command
+{
+
+ public String[] getRequiredMemberships()
+ {
+ return null;
+ }
+
+ abstract public void execute(WebAppController controller, HttpServletRequest req,
HttpServletResponse res)
+ throws Exception;
+}
\ No newline at end of file
Added:
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/web/command/CommandHandler.java
===================================================================
---
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/web/command/CommandHandler.java
(rev 0)
+++
exo/portal/branches/3.1.x/component/web/src/main/java/org/exoplatform/web/command/CommandHandler.java 2010-08-10
03:24:42 UTC (rev 3784)
@@ -0,0 +1,196 @@
+/**
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.web.command;
+
+import org.exoplatform.web.WebAppController;
+import org.exoplatform.web.WebRequestHandler;
+
+import java.lang.reflect.Array;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.rmi.activation.UnknownObjectException;
+import java.util.Iterator;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * Created by The eXo Platform SAS
+ * Mar 21, 2007
+ */
+public class CommandHandler extends WebRequestHandler
+{
+
+ public String[] getPath()
+ {
+ return new String[]{"/command"};
+ }
+
+ public void execute(WebAppController controller, HttpServletRequest req,
HttpServletResponse res) throws Exception
+ {
+ Map props = req.getParameterMap();
+ String type = req.getParameter("type");
+ if (!("org.exoplatform.web.command.handler.UploadHandler".equals(type)
||
+
"org.exoplatform.web.command.handler.DownloadHandler".equals(type)))
+ throw new IllegalArgumentException("Unknown type command handler");
+ Command command = createCommand(type, props);
+ if (command == null)
+ throw new UnknownObjectException("Unknown command handler with type is
" + type);
+ command.execute(controller, req, res);
+ }
+
+ /**
+ * This method should use the java reflection to create the command object according
to the command
+ * type, then populate the command properties
+ *
+ * @param type The command class type
+ * @param props list of the properties that should be set in the command object
+ * @return The command object instance
+ * @throws Exception
+ */
+ @SuppressWarnings("unchecked")
+ public Command createCommand(String type, Map props) throws Exception
+ {
+ ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+ Class<?> clazz = classLoader.loadClass(type);
+ Object object = clazz.newInstance();
+ Iterator<Object> iter = props.keySet().iterator();
+ while (iter.hasNext())
+ {
+ Object key = iter.next();
+ Field field = getField(clazz, key.toString());
+ if (field == null)
+ continue;
+ setValue(object, field, props.get(key));
+ }
+ return (Command)object;
+ }
+
+ private final void setValue(Object bean, Field field, Object value) throws Exception
+ {
+ Class type = field.getType();
+ if (type.isArray() && !value.getClass().isArray())
+ {
+ value = toValues(type, new Object[]{value});
+ }
+ else if (type.isArray() && value.getClass().isArray())
+ {
+ value = toValues(type, value);
+ }
+ else
+ {
+ if (!type.isArray() && value.getClass().isArray())
+ value = Array.get(value, 0);
+ value = toValue(type, value);
+ }
+ Class clazz = bean.getClass();
+ Method method = getMethod("set", field, clazz);
+ if (method != null)
+ {
+ method.invoke(bean, new Object[]{value});
+ return;
+ }
+ field.setAccessible(true);
+ field.set(bean, value);
+ }
+
+ private final Method getMethod(String prefix, Field field, Class clazz) throws
Exception
+ {
+ StringBuilder name = new StringBuilder(field.getName());
+ name.setCharAt(0, Character.toUpperCase(name.charAt(0)));
+ name.insert(0, prefix);
+ return getMethodByName(name.toString(), field, clazz);
+ }
+
+ private final Method getMethodByName(String name, Field field, Class clazz)
+ {
+ try
+ {
+ Method method = clazz.getDeclaredMethod(name.toString(), new
Class[]{field.getType()});
+ if (method != null)
+ return method;
+ if (clazz == Object.class)
+ return null;
+ method = getMethodByName(name, field, clazz.getSuperclass());
+ if (method != null)
+ return method;
+ }
+ catch (Exception e)
+ {
+ }
+ return null;
+ }
+
+ private Field getField(Class clazz, String name)
+ {
+ Field field = null;
+ try
+ {
+ field = clazz.getDeclaredField(name);
+ }
+ catch (Exception e)
+ {
+ }
+ if (field != null)
+ return field;
+ if (clazz == Object.class)
+ return null;
+ return getField(clazz.getSuperclass(), name);
+ }
+
+ private Object toValues(Class<?> clazz, Object objects)
+ {
+ Class componentType = clazz.getComponentType();
+ Object newValues = Array.newInstance(componentType, Array.getLength(objects));
+ for (int i = 0; i < Array.getLength(objects); i++)
+ {
+ Array.set(newValues, i, toValue(componentType, Array.get(objects, i)));
+ }
+ return clazz.cast(newValues);
+ }
+
+ private Object toValue(Class<?> clazz, Object object)
+ {
+ if (clazz == int.class)
+ return new Integer(object.toString()).intValue();
+ if (clazz == short.class)
+ return new Short(object.toString()).shortValue();
+ if (clazz == float.class)
+ return new Float(object.toString()).floatValue();
+ if (clazz == double.class)
+ return new Double(object.toString()).doubleValue();
+ if (clazz == boolean.class)
+ return new Boolean(object.toString()).booleanValue();
+ if (clazz == char.class)
+ return object.toString().trim().charAt(0);
+ try
+ {
+ Constructor<?> constructor = clazz.getConstructor(new
Class[]{String.class});
+ return constructor.newInstance(new Object[]{object.toString()});
+ }
+ catch (Exception e)
+ {
+ }
+ return object.toString();
+ }
+
+}
\ No newline at end of file
Added:
exo/portal/branches/3.1.x/component/web/src/test/java/org/exoplatform/web/command/TestCommandHandler.java
===================================================================
---
exo/portal/branches/3.1.x/component/web/src/test/java/org/exoplatform/web/command/TestCommandHandler.java
(rev 0)
+++
exo/portal/branches/3.1.x/component/web/src/test/java/org/exoplatform/web/command/TestCommandHandler.java 2010-08-10
03:24:42 UTC (rev 3784)
@@ -0,0 +1,98 @@
+/**
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.web.command;
+
+import org.exoplatform.component.test.AbstractGateInTest;
+import org.exoplatform.web.WebAppController;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * Created by The eXo Platform SARL
+ * Author : Nhu Dinh Thuan
+ * nhudinhthuan(a)exoplatform.com
+ * Mar 26, 2007
+ */
+public class TestCommandHandler extends AbstractGateInTest
+{
+
+ public TestCommandHandler(String name)
+ {
+ super(name);
+ }
+
+ public void testCommandHandler() throws Exception
+ {
+ CommandHandler handler = new CommandHandler();
+ Map<String, Object> props = new HashMap<String, Object>();
+ props.put("intProp", "10");
+ props.put("stringProp", "welcome to eXo");
+ props.put("doubleValues", new String[]{"10.0",
"-6.7", "7.0"});
+ props.put("booleanValue", "true");
+ Command command =
handler.createCommand("org.exoplatform.web.command.TestCommandHandler$CommandTest2",
props);
+ command.execute(null, null, null);
+ }
+
+ static public class CommandTest2 extends CommandTest
+ {
+
+ private boolean booleanValue = false;
+
+ public void execute(WebAppController controller, HttpServletRequest req,
HttpServletResponse res)
+ throws Exception
+ {
+ super.execute(controller, req, res);
+ System.out.println(" \n\n\n === >" + booleanValue +
"\n\n");
+ }
+ }
+
+ static public class CommandTest extends Command
+ {
+
+ private double[] doubleValues;
+
+ private Integer intProp;
+
+ private String stringProp;
+
+ public void execute(WebAppController controller, HttpServletRequest req,
HttpServletResponse res)
+ throws Exception
+ {
+ System.out.println("\n\n");
+ System.out.println("int prop : " + intProp);
+ System.out.println("String prop : " + stringProp);
+ for (double ele : doubleValues)
+ {
+ System.out.println("===== > " + ele);
+ }
+ System.out.println("\n\n");
+ }
+
+ public void setStringProp(String value)
+ {
+ System.out.println("\n\n invoke setter " + value +
"\n\n");
+ stringProp = value;
+ }
+ }
+}
Modified: exo/portal/branches/3.1.x/examples/portal/war/src/main/webapp/WEB-INF/web.xml
===================================================================
---
exo/portal/branches/3.1.x/examples/portal/war/src/main/webapp/WEB-INF/web.xml 2010-08-10
03:03:07 UTC (rev 3783)
+++
exo/portal/branches/3.1.x/examples/portal/war/src/main/webapp/WEB-INF/web.xml 2010-08-10
03:24:42 UTC (rev 3784)
@@ -220,14 +220,10 @@
<url-pattern>/service</url-pattern>
</servlet-mapping>
<servlet-mapping>
- <servlet-name>portal</servlet-name>
- <url-pattern>/upload/*</url-pattern>
+ <servlet-name>portal</servlet-name>
+ <url-pattern>/command/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
- <servlet-name>portal</servlet-name>
- <url-pattern>/download/*</url-pattern>
- </servlet-mapping>
- <servlet-mapping>
<servlet-name>RestServer</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
Modified:
exo/portal/branches/3.1.x/testsuite/webuibasedsamples/src/main/java/org/exoplatform/sample/webui/component/UISampleDownloadUpload.java
===================================================================
---
exo/portal/branches/3.1.x/testsuite/webuibasedsamples/src/main/java/org/exoplatform/sample/webui/component/UISampleDownloadUpload.java 2010-08-10
03:03:07 UTC (rev 3783)
+++
exo/portal/branches/3.1.x/testsuite/webuibasedsamples/src/main/java/org/exoplatform/sample/webui/component/UISampleDownloadUpload.java 2010-08-10
03:24:42 UTC (rev 3784)
@@ -30,8 +30,8 @@
public UISampleDownloadUpload() throws Exception
{
addUIFormInput(new UIFormUploadInput("name0", "value0"));
- addUIFormInput(new UIFormUploadInput("name1", "value1", 100));
- addUIFormInput(new UIFormUploadInput("name2", "value2", 200));
+ addUIFormInput(new UIFormUploadInput("name1", "value1"));
+ addUIFormInput(new UIFormUploadInput("name2", "value2"));
}
public void setDownloadLink(String[] downloadLink)
Modified:
exo/portal/branches/3.1.x/web/eXoResources/src/main/webapp/javascript/eXo/webui/UIUpload.js
===================================================================
---
exo/portal/branches/3.1.x/web/eXoResources/src/main/webapp/javascript/eXo/webui/UIUpload.js 2010-08-10
03:03:07 UTC (rev 3783)
+++
exo/portal/branches/3.1.x/web/eXoResources/src/main/webapp/javascript/eXo/webui/UIUpload.js 2010-08-10
03:24:42 UTC (rev 3784)
@@ -28,8 +28,8 @@
* @param {boolean} isAutoUpload auto upload or none
*/
UIUpload.prototype.initUploadEntry = function(uploadId, isAutoUpload) {
- var url = eXo.env.server.context + "/upload?" ;
- url += "action=progress&uploadId="+uploadId ;
+ var url = eXo.env.server.context + "/command?" ;
+ url +=
"type=org.exoplatform.web.command.handler.UploadHandler&action=progress&uploadId="+uploadId
;
var responseText = ajaxAsyncGetRequest(url, false);
var response;
@@ -51,8 +51,9 @@
UIUpload.prototype.createUploadEntry = function(uploadId, isAutoUpload) {
var iframe = document.getElementById(uploadId+'uploadFrame');
var idoc = iframe.contentWindow.document ;
- var uploadAction = eXo.env.server.context + "/upload?" ;
- uploadAction += "uploadId=" + uploadId+"&action=upload" ;
+ var uploadAction = eXo.env.server.context + "/command?" ;
+ uploadAction += "type=org.exoplatform.web.command.handler.UploadHandler";
+ uploadAction += "&uploadId=" + uploadId+"&action=upload" ;
idoc.open();
idoc.write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>");
idoc.write("<html
xmlns='http://www.w3.org/1999/xhtml'
xml:lang='en' lang='" +eXo.core.I18n.lang+ "' dir='"
+eXo.core.I18n.dir+ "'>");
@@ -84,8 +85,8 @@
UIUpload.prototype.refeshProgress = function(elementId) {
var list = eXo.webui.UIUpload.listUpload;
if(list.length < 1) return;
- var url = eXo.env.server.context + "/upload?" ;
- url += "action=progress" ;
+ var url = eXo.env.server.context + "/command?" ;
+ url +=
"type=org.exoplatform.web.command.handler.UploadHandler&action=progress" ;
// var url = eXo.env.server.context + "/upload?action=progress";
for(var i = 0; i < list.length; i++){
url = url + "&uploadId=" + list[i];
@@ -164,8 +165,8 @@
*/
UIUpload.prototype.abortUpload = function(id) {
eXo.webui.UIUpload.listUpload.remove(id);
- var url = eXo.env.server.context + "/upload?" ;
- url += "uploadId=" +id+"&action=abort" ;
+ var url = eXo.env.server.context + "/command?" ;
+ url += "type=org.exoplatform.web.command.handler.UploadHandler&uploadId="
+id+"&action=abort" ;
// var url = eXo.env.server.context + "/upload?uploadId="
+id+"&action=abort" ;
var request = eXo.core.Browser.createHttpRequest();
request.open('GET', url, false);
@@ -196,8 +197,8 @@
* @param {String} id upload identifier
*/
UIUpload.prototype.deleteUpload = function(id) {
- var url = eXo.env.server.context + "/upload?";
- url += "uploadId=" +id+"&action=delete" ;
+ var url = eXo.env.server.context + "/command?";
+ url += "type=org.exoplatform.web.command.handler.UploadHandler&uploadId="
+id+"&action=delete" ;
// var url = eXo.env.server.context + "/upload?uploadId="
+id+"&action=delete" ;
var request = eXo.core.Browser.createHttpRequest();
request.open('GET', url, false);
Modified: exo/portal/branches/3.1.x/web/portal/src/main/webapp/WEB-INF/web.xml
===================================================================
--- exo/portal/branches/3.1.x/web/portal/src/main/webapp/WEB-INF/web.xml 2010-08-10
03:03:07 UTC (rev 3783)
+++ exo/portal/branches/3.1.x/web/portal/src/main/webapp/WEB-INF/web.xml 2010-08-10
03:24:42 UTC (rev 3784)
@@ -267,12 +267,8 @@
</servlet-mapping>
<servlet-mapping>
<servlet-name>portal</servlet-name>
- <url-pattern>/upload/*</url-pattern>
+ <url-pattern>/command/*</url-pattern>
</servlet-mapping>
- <servlet-mapping>
- <servlet-name>portal</servlet-name>
- <url-pattern>/download/*</url-pattern>
- </servlet-mapping>
<servlet-mapping>
<servlet-name>RestServer</servlet-name>
<url-pattern>/rest/*</url-pattern>
Modified:
exo/portal/branches/3.1.x/webui/core/src/main/java/org/exoplatform/webui/form/UIFormUploadInput.java
===================================================================
---
exo/portal/branches/3.1.x/webui/core/src/main/java/org/exoplatform/webui/form/UIFormUploadInput.java 2010-08-10
03:03:07 UTC (rev 3783)
+++
exo/portal/branches/3.1.x/webui/core/src/main/java/org/exoplatform/webui/form/UIFormUploadInput.java 2010-08-10
03:24:42 UTC (rev 3784)
@@ -54,6 +54,11 @@
*/
private boolean isAutoUpload = false;
+ /**
+ * The maximum size of the file to upload, in MB
+ */
+ //private int limitMB_;
+
public UIFormUploadInput(String name, String bindingExpression)
{
super(name, bindingExpression, String.class);
@@ -122,8 +127,9 @@
WebuiRequestContext pcontext =
(WebuiRequestContext)context.getParentAppRequestContext();
if (pcontext == null)
pcontext = context;
- String uploadAction = pcontext.getRequestContextPath() + "/upload?";
- uploadAction += "uploadId=" + uploadId_ +
"&action=upload";
+ String uploadAction = pcontext.getRequestContextPath() + "/command?";
+ uploadAction +=
"type=org.exoplatform.web.command.handler.UploadHandler";
+ uploadAction += "&uploadId=" + uploadId_ +
"&action=upload";
return uploadAction;
}
@@ -142,4 +148,8 @@
this.isAutoUpload = isAutoUpload;
}
+ // public void setLimit(int size) { limitMB_ = size; }
+ //
+ // public int getLimit() { return limitMB_; }
+
}
\ No newline at end of file
Modified:
exo/portal/branches/3.1.x/webui/eXo/src/main/java/org/exoplatform/web/command/handler/DownloadHandler.java
===================================================================
---
exo/portal/branches/3.1.x/webui/eXo/src/main/java/org/exoplatform/web/command/handler/DownloadHandler.java 2010-08-10
03:03:07 UTC (rev 3783)
+++
exo/portal/branches/3.1.x/webui/eXo/src/main/java/org/exoplatform/web/command/handler/DownloadHandler.java 2010-08-10
03:24:42 UTC (rev 3784)
@@ -24,7 +24,7 @@
import org.exoplatform.download.DownloadResource;
import org.exoplatform.download.DownloadService;
import org.exoplatform.web.WebAppController;
-import org.exoplatform.web.WebRequestHandler;
+import org.exoplatform.web.command.Command;
import java.io.InputStream;
import java.io.OutputStream;
@@ -39,18 +39,14 @@
* thuy.le(a)exoplatform.com
* Dec 9, 2006
*/
-public class DownloadHandler extends WebRequestHandler
+public class DownloadHandler extends Command
{
- @Override
- public String[] getPath()
- {
- return new String[]{"/download"};
- }
+ private String resourceId;
+ @SuppressWarnings("unused")
public void execute(WebAppController controller, HttpServletRequest req,
HttpServletResponse res) throws Exception
{
- String resourceId = req.getParameter("resourceId");
res.setHeader("Cache-Control", "private max-age=600,
s-maxage=120");
ExoContainer container = ExoContainerContext.getCurrentContainer();
DownloadService dservice =
(DownloadService)container.getComponentInstanceOfType(DownloadService.class);
@@ -91,6 +87,11 @@
}
}
+ public String getResourceId()
+ {
+ return resourceId;
+ }
+
private static void optimalRead(InputStream is, OutputStream os) throws Exception
{
int bufferLength = 1024; //TODO: Better to compute bufferLength in term of -Xms,
-Xmx properties
Added:
exo/portal/branches/3.1.x/webui/eXo/src/main/java/org/exoplatform/web/command/handler/GetApplicationHandler.java
===================================================================
---
exo/portal/branches/3.1.x/webui/eXo/src/main/java/org/exoplatform/web/command/handler/GetApplicationHandler.java
(rev 0)
+++
exo/portal/branches/3.1.x/webui/eXo/src/main/java/org/exoplatform/web/command/handler/GetApplicationHandler.java 2010-08-10
03:24:42 UTC (rev 3784)
@@ -0,0 +1,169 @@
+/**
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.web.command.handler;
+
+import org.exoplatform.application.registry.Application;
+import org.exoplatform.application.registry.ApplicationCategory;
+import org.exoplatform.application.registry.ApplicationRegistryService;
+import org.exoplatform.container.ExoContainer;
+import org.exoplatform.container.ExoContainerContext;
+import org.exoplatform.json.BeanToJSONPlugin;
+import org.exoplatform.json.JSONService;
+import org.exoplatform.portal.config.model.ApplicationType;
+import org.exoplatform.web.WebAppController;
+import org.exoplatform.web.command.Command;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.List;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * Created by The eXo Platform SARL
+ * Author : Nhu Dinh Thuan
+ * nhudinhthuan(a)exoplatform.com
+ * May 31, 2007
+ */
+public class GetApplicationHandler extends Command
+{
+
+ private ApplicationType<?>[] applicationType;
+
+ public void setApplicationTypes(ApplicationType<?>[] type)
+ {
+ applicationType = type;
+ }
+
+ @SuppressWarnings("unused")
+ public void execute(WebAppController controller, HttpServletRequest req,
HttpServletResponse res) throws Exception
+ {
+ res.setHeader("Cache-Control", "no-cache");
+ Writer writer = res.getWriter();
+ try
+ {
+ writer.append(getApplications(req.getRemoteUser()));
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ throw new IOException(e.getMessage());
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ private StringBuilder getApplications(String remoteUser) throws Exception
+ {
+ ExoContainer container = ExoContainerContext.getCurrentContainer();
+ ApplicationRegistryService prService =
+
(ApplicationRegistryService)container.getComponentInstanceOfType(ApplicationRegistryService.class);
+
+ if (applicationType == null)
+ applicationType = new ApplicationType<?>[0];
+ List<ApplicationCategory> appCategories =
prService.getApplicationCategories(remoteUser, applicationType);
+ ApplicationCategoryToJSONPlugin toJSON = new ApplicationCategoryToJSONPlugin();
+
+ StringBuilder value = new StringBuilder();
+ JSONService jsonService = new JSONService();
+ jsonService.register(ApplicationCategory.class, toJSON);
+
+ if (appCategories.size() < 1)
+ return value;
+
+ value.append("{\n").append(" applicationRegistry : {\n");
+ for (int i = 0; i < appCategories.size(); i++)
+ {
+ ApplicationCategory category = appCategories.get(i);
+ jsonService.toJSONScript(category, value, 1);
+ if (i < appCategories.size() - 1)
+ value.append(" ,\n");
+ }
+ value.append(" }\n").append("}\n");
+
+ return value;
+ }
+
+ private class ApplicationCategoryToJSONPlugin extends
BeanToJSONPlugin<ApplicationCategory>
+ {
+
+ @SuppressWarnings("unchecked")
+ public void toJSONScript(ApplicationCategory category, StringBuilder builder, int
indentLevel) throws Exception
+ {
+ StringBuilder builderPortlet = toJSONScript(category, indentLevel + 2);
+ if (builderPortlet.length() < 1)
+ return;
+
+ appendIndentation(builder, indentLevel);
+ builder.append('\'').append(category.getName()).append("' :
{\n");
+ appendIndentation(builder, indentLevel + 1);
+ builder.append("'name' :
'").append(category.getDisplayName()).append("',\n");
+ appendIndentation(builder, indentLevel + 1);
+ builder.append("'applications' : {\n");
+ builder.append(builderPortlet);
+ appendIndentation(builder, indentLevel + 1);
+ builder.append("}\n");
+ appendIndentation(builder, indentLevel);
+ builder.append("}\n");
+ }
+
+ @SuppressWarnings("unchecked")
+ private StringBuilder toJSONScript(ApplicationCategory category, int indentLevel)
throws Exception
+ {
+ StringBuilder builder = new StringBuilder();
+ List<Application> applications = category.getApplications();
+
+ for (int j = 0; j < applications.size(); j++)
+ {
+ toJSONScript(applications.get(j), builder, indentLevel);
+ if (j < applications.size() - 1)
+ {
+ appendIndentation(builder, indentLevel);
+ builder.append(",\n");
+ }
+ }
+
+ return builder;
+ }
+
+ private void toJSONScript(Application application, StringBuilder builder, int
indentLevel)
+ {
+ appendIndentation(builder, indentLevel);
+ builder.append('\'').append(application.getId()).append("'
: {\n");
+ appendIndentation(builder, indentLevel + 1);
+ //TODO: Tung.Pham modified
+ //----------------------------
+ //builder.append("'title' :
").append("'").append(application.getApplicationName()).append("',\n");
+ builder.append("'name' :
").append("'").append(application.getApplicationName()).append("',\n");
+ appendIndentation(builder, indentLevel + 1);
+ builder.append("'title' :
").append("'").append(application.getDisplayName()).append("',\n");
+ //------------------------------
+ appendIndentation(builder, indentLevel + 1);
+ builder.append("'des' :
").append("'").append(application.getDescription()).append("',\n");
+ appendIndentation(builder, indentLevel + 1);
+ builder.append("'id' :
").append("'").append(application.getId()).append("',\n");
+ appendIndentation(builder, indentLevel + 1);
+ builder.append("'type' :
").append("'").append(application.getType()).append("'\n");
+ appendIndentation(builder, indentLevel);
+ builder.append("}\n");
+ }
+ }
+
+}
Added:
exo/portal/branches/3.1.x/webui/eXo/src/main/java/org/exoplatform/web/command/handler/HelloJCRHandler.java
===================================================================
---
exo/portal/branches/3.1.x/webui/eXo/src/main/java/org/exoplatform/web/command/handler/HelloJCRHandler.java
(rev 0)
+++
exo/portal/branches/3.1.x/webui/eXo/src/main/java/org/exoplatform/web/command/handler/HelloJCRHandler.java 2010-08-10
03:24:42 UTC (rev 3784)
@@ -0,0 +1,46 @@
+/**
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.web.command.handler;
+
+import org.exoplatform.web.WebAppController;
+import org.exoplatform.web.command.Command;
+
+import java.io.PrintWriter;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * Created by The eXo Platform SARL
+ * Author : Nguyen Ba Uoc
+ * thuy.le(a)exoplatform.com
+ * July 24, 2007
+ */
+public class HelloJCRHandler extends Command
+{
+
+ public void execute(WebAppController controller, HttpServletRequest req,
HttpServletResponse res) throws Exception
+ {
+ res.setContentType("text/xml");
+ PrintWriter out = res.getWriter();
+ out.println("Hello from server");
+ System.out.println("Client request");
+ }
+}
\ No newline at end of file
Modified:
exo/portal/branches/3.1.x/webui/eXo/src/main/java/org/exoplatform/web/command/handler/UploadHandler.java
===================================================================
---
exo/portal/branches/3.1.x/webui/eXo/src/main/java/org/exoplatform/web/command/handler/UploadHandler.java 2010-08-10
03:03:07 UTC (rev 3783)
+++
exo/portal/branches/3.1.x/webui/eXo/src/main/java/org/exoplatform/web/command/handler/UploadHandler.java 2010-08-10
03:24:42 UTC (rev 3784)
@@ -24,7 +24,7 @@
import org.exoplatform.upload.UploadResource;
import org.exoplatform.upload.UploadService;
import org.exoplatform.web.WebAppController;
-import org.exoplatform.web.WebRequestHandler;
+import org.exoplatform.web.command.Command;
import java.io.Writer;
import java.net.URLEncoder;
@@ -38,24 +38,30 @@
* nhudinhthuan(a)exoplatform.com
* Dec 9, 2006
*/
-public class UploadHandler extends WebRequestHandler
+public class UploadHandler extends Command
{
static enum UploadServiceAction {
PROGRESS, UPLOAD, DELETE, ABORT
}
-
- @Override
- public String[] getPath()
+
+ private String action;
+
+ private String[] uploadId;
+
+ public void setAction(String action)
{
- return new String[]{"/upload"};
+ this.action = action;
}
+ public void setUploadId(String[] uploadId)
+ {
+ this.uploadId = uploadId;
+ }
+
+ @SuppressWarnings("unused")
public void execute(WebAppController controller, HttpServletRequest req,
HttpServletResponse res) throws Exception
{
- String action = req.getParameter("action");
- String[] uploadIds = req.getParameterValues("uploadId");
-
res.setHeader("Cache-Control", "no-cache");
ExoContainer container = ExoContainerContext.getCurrentContainer();
@@ -67,19 +73,19 @@
if (uploadActionService == UploadServiceAction.PROGRESS)
{
Writer writer = res.getWriter();
- if (uploadIds == null)
+ if (uploadId == null)
return;
StringBuilder value = new StringBuilder();
value.append("{\n upload : {");
- for (int i = 0; i < uploadIds.length; i++)
+ for (int i = 0; i < uploadId.length; i++)
{
- UploadResource upResource = service.getUploadResource(uploadIds[i]);
+ UploadResource upResource = service.getUploadResource(uploadId[i]);
if (upResource == null)
continue;
if (upResource.getStatus() == UploadResource.FAILED_STATUS)
{
- int limitMB = service.getUploadLimitsMB().get(uploadIds[i]).intValue();
- value.append("\n
\"").append(uploadIds[i]).append("\": {");
+ int limitMB = service.getUploadLimitsMB().get(uploadId[i]).intValue();
+ value.append("\n
\"").append(uploadId[i]).append("\": {");
value.append("\n
\"status\":").append('\"').append("failed").append("\",");
value.append("\n
\"size\":").append('\"').append(limitMB).append("\"");
value.append("\n }");
@@ -90,12 +96,12 @@
{
percent = (upResource.getUploadedSize() * 100) /
upResource.getEstimatedSize();
}
- value.append("\n
\"").append(uploadIds[i]).append("\": {");
+ value.append("\n
\"").append(uploadId[i]).append("\": {");
value.append("\n
\"percent\":").append('\"').append((int)percent).append("\",");
value.append("\n
\"fileName\":").append('\"').append(encodeName(upResource.getFileName()))
.append("\"");
value.append("\n }");
- if (i < uploadIds.length - 1)
+ if (i < uploadId.length - 1)
value.append(',');
}
value.append("\n }\n}");
@@ -107,11 +113,14 @@
}
else if (uploadActionService == UploadServiceAction.DELETE)
{
- service.removeUploadResource(uploadIds[0]);
+ service.removeUpload(uploadId[0]);
}
else if (uploadActionService == UploadServiceAction.ABORT)
{
- service.removeUploadResource(uploadIds[0]);
+ //TODO: dang.tung - we don't need 2 statements because it'll show error
when we reload browser
+ //UploadResource upResource = service.getUploadResource(uploadId[0]);
+ //if(upResource != null) upResource.setStatus(UploadResource.UPLOADED_STATUS) ;
+ service.removeUpload(uploadId[0]);
}
}