001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.dynamicdatalists.action;
016    
017    import com.liferay.portal.kernel.json.JSONFactoryUtil;
018    import com.liferay.portal.kernel.json.JSONObject;
019    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
020    import com.liferay.portal.kernel.util.MimeTypesUtil;
021    import com.liferay.portal.kernel.util.ParamUtil;
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.service.DDLRecordLocalServiceUtil;
029    
030    import java.io.InputStream;
031    import java.io.Serializable;
032    
033    import javax.portlet.ActionRequest;
034    import javax.portlet.ActionResponse;
035    import javax.portlet.PortletConfig;
036    
037    import javax.servlet.http.HttpServletRequest;
038    import javax.servlet.http.HttpServletResponse;
039    
040    import org.apache.struts.action.ActionForm;
041    import org.apache.struts.action.ActionForward;
042    import org.apache.struts.action.ActionMapping;
043    
044    /**
045     * @author Brian Wing Shun Chan
046     * @author Bruno Basto
047     */
048    public class GetFileUploadAction extends PortletAction {
049    
050            @Override
051            public void processAction(
052                            ActionMapping mapping, ActionForm form,
053                            PortletConfig portletConfig, ActionRequest actionRequest,
054                            ActionResponse actionResponse)
055                    throws Exception {
056    
057                    long recordId = ParamUtil.getLong(actionRequest, "recordId");
058                    String fieldName = ParamUtil.getString(actionRequest, "fieldName");
059    
060                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
061                            actionRequest);
062                    HttpServletResponse response = PortalUtil.getHttpServletResponse(
063                            actionResponse);
064    
065                    try {
066                            getFile(recordId, fieldName, request, response);
067    
068                            setForward(actionRequest, ActionConstants.COMMON_NULL);
069                    }
070                    catch (Exception e) {
071                            PortalUtil.sendError(e, actionRequest, actionResponse);
072                    }
073            }
074    
075            @Override
076            public ActionForward strutsExecute(
077                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
078                            HttpServletResponse response)
079                    throws Exception {
080    
081                    long recordId = ParamUtil.getLong(request, "recordId");
082                    String fieldName = ParamUtil.getString(request, "fieldName");
083    
084                    try {
085                            getFile(recordId, fieldName, request, response);
086                    }
087                    catch (Exception e) {
088                            PortalUtil.sendError(e, request, response);
089                    }
090    
091                    return null;
092            }
093    
094            protected void getFile(
095                            long recordId, String fieldName, HttpServletRequest request,
096                            HttpServletResponse response)
097                    throws Exception {
098    
099                    DDLRecord record = DDLRecordLocalServiceUtil.getRecord(recordId);
100    
101                    Serializable fieldValue = record.getFieldValue(fieldName);
102    
103                    JSONObject fileJSONObject = JSONFactoryUtil.createJSONObject(
104                            String.valueOf(fieldValue));
105    
106                    String fileName = fileJSONObject.getString("name");
107                    String filePath = fileJSONObject.getString("path");
108    
109                    InputStream is = DLStoreUtil.getFileAsStream(
110                            record.getCompanyId(), CompanyConstants.SYSTEM, filePath);
111                    long contentLength = DLStoreUtil.getFileSize(
112                            record.getCompanyId(), CompanyConstants.SYSTEM, filePath);
113                    String contentType = MimeTypesUtil.getContentType(fileName);
114    
115                    ServletResponseUtil.sendFile(
116                            request, response, fileName, is, contentLength, contentType);
117            }
118    
119            @Override
120            protected boolean isCheckMethodOnProcessAction() {
121                    return _CHECK_METHOD_ON_PROCESS_ACTION;
122            }
123    
124            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
125    
126    }