001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.dynamicdatalists.action;
016    
017    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
018    import com.liferay.portal.kernel.util.MimeTypesUtil;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.model.CompanyConstants;
023    import com.liferay.portal.struts.ActionConstants;
024    import com.liferay.portal.struts.PortletAction;
025    import com.liferay.portal.util.PortalUtil;
026    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
027    import com.liferay.portlet.dynamicdatalists.model.DDLRecord;
028    import com.liferay.portlet.dynamicdatalists.model.DDLRecordVersion;
029    import com.liferay.portlet.dynamicdatalists.service.DDLRecordLocalServiceUtil;
030    import com.liferay.portlet.dynamicdatalists.util.DDLUtil;
031    
032    import java.io.InputStream;
033    
034    import javax.portlet.ActionRequest;
035    import javax.portlet.ActionResponse;
036    import javax.portlet.PortletConfig;
037    
038    import javax.servlet.http.HttpServletRequest;
039    import javax.servlet.http.HttpServletResponse;
040    
041    import org.apache.struts.action.ActionForm;
042    import org.apache.struts.action.ActionForward;
043    import org.apache.struts.action.ActionMapping;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     * @author Bruno Basto
048     */
049    public class GetFileUploadAction extends PortletAction {
050    
051            @Override
052            public void processAction(
053                            ActionMapping mapping, ActionForm form,
054                            PortletConfig portletConfig, ActionRequest actionRequest,
055                            ActionResponse actionResponse)
056                    throws Exception {
057    
058                    long recordId = ParamUtil.getLong(actionRequest, "recordId");
059                    String fieldName = ParamUtil.getString(actionRequest, "fieldName");
060                    String fileName = ParamUtil.getString(actionRequest, "fileName");
061    
062                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
063                            actionRequest);
064                    HttpServletResponse response = PortalUtil.getHttpServletResponse(
065                            actionResponse);
066    
067                    try {
068                            getFile(recordId, fieldName, fileName, request, response);
069    
070                            setForward(actionRequest, ActionConstants.COMMON_NULL);
071                    }
072                    catch (Exception e) {
073                            PortalUtil.sendError(e, actionRequest, actionResponse);
074                    }
075            }
076    
077            @Override
078            public ActionForward strutsExecute(
079                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
080                            HttpServletResponse response)
081                    throws Exception {
082    
083                    long recordId = ParamUtil.getLong(request, "recordId");
084                    String fieldName = ParamUtil.getString(request, "fieldName");
085                    String fileName = ParamUtil.getString(request, "fileName");
086    
087                    try {
088                            getFile(recordId, fieldName, fileName, request, response);
089                    }
090                    catch (Exception e) {
091                            PortalUtil.sendError(e, request, response);
092                    }
093    
094                    return null;
095            }
096    
097            protected void getFile(
098                            long recordId, String fieldName, String fileName,
099                            HttpServletRequest request, HttpServletResponse response)
100                    throws Exception {
101    
102                    DDLRecord record = DDLRecordLocalServiceUtil.getRecord(recordId);
103    
104                    DDLRecordVersion recordVersion = record.getLatestRecordVersion();
105    
106                    StringBundler sb = new StringBundler(5);
107    
108                    sb.append(DDLUtil.getRecordFileUploadPath(record));
109                    sb.append(StringPool.SLASH);
110                    sb.append(recordVersion.getVersion());
111                    sb.append(StringPool.SLASH);
112                    sb.append(fieldName);
113    
114                    String path = sb.toString();
115    
116                    InputStream is = DLStoreUtil.getFileAsStream(
117                            record.getCompanyId(), CompanyConstants.SYSTEM, path);
118                    long contentLength = DLStoreUtil.getFileSize(
119                            record.getCompanyId(), CompanyConstants.SYSTEM, path);
120                    String contentType = MimeTypesUtil.getContentType(fileName);
121    
122                    ServletResponseUtil.sendFile(
123                            request, response, fileName, is, contentLength, contentType);
124            }
125    
126            @Override
127            protected boolean isCheckMethodOnProcessAction() {
128                    return _CHECK_METHOD_ON_PROCESS_ACTION;
129            }
130    
131            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
132    
133    }