001    /**
002     * Copyright (c) 2000-2013 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.portal.editor.fckeditor.receiver.impl;
016    
017    import com.liferay.portal.editor.fckeditor.command.CommandArgument;
018    import com.liferay.portal.editor.fckeditor.exception.FCKException;
019    import com.liferay.portal.editor.fckeditor.receiver.CommandReceiver;
020    import com.liferay.portal.kernel.dao.orm.QueryUtil;
021    import com.liferay.portal.kernel.io.ByteArrayFileInputStream;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.util.CharPool;
025    import com.liferay.portal.kernel.util.ContentTypes;
026    import com.liferay.portal.kernel.util.GetterUtil;
027    import com.liferay.portal.kernel.util.HtmlUtil;
028    import com.liferay.portal.kernel.util.MimeTypesUtil;
029    import com.liferay.portal.kernel.util.ParamUtil;
030    import com.liferay.portal.kernel.util.PropsKeys;
031    import com.liferay.portal.kernel.util.StreamUtil;
032    import com.liferay.portal.kernel.util.StringBundler;
033    import com.liferay.portal.kernel.util.StringPool;
034    import com.liferay.portal.kernel.util.StringUtil;
035    import com.liferay.portal.kernel.util.UniqueList;
036    import com.liferay.portal.kernel.util.Validator;
037    import com.liferay.portal.model.Group;
038    import com.liferay.portal.model.Layout;
039    import com.liferay.portal.model.Organization;
040    import com.liferay.portal.security.xml.SecureXMLFactoryProviderUtil;
041    import com.liferay.portal.service.GroupLocalServiceUtil;
042    import com.liferay.portal.service.OrganizationLocalServiceUtil;
043    import com.liferay.portal.theme.ThemeDisplay;
044    import com.liferay.portal.upload.LiferayFileItem;
045    import com.liferay.portal.upload.LiferayFileItemFactory;
046    import com.liferay.portal.upload.LiferayFileUpload;
047    import com.liferay.portal.upload.LiferayServletRequest;
048    import com.liferay.portal.upload.UploadServletRequestImpl;
049    import com.liferay.portal.util.PrefsPropsUtil;
050    import com.liferay.portal.util.PropsValues;
051    
052    import java.io.InputStream;
053    import java.io.PrintWriter;
054    
055    import java.util.HashMap;
056    import java.util.LinkedHashMap;
057    import java.util.List;
058    import java.util.Map;
059    
060    import javax.servlet.http.HttpServletRequest;
061    import javax.servlet.http.HttpServletResponse;
062    
063    import javax.xml.parsers.DocumentBuilder;
064    import javax.xml.parsers.DocumentBuilderFactory;
065    import javax.xml.parsers.ParserConfigurationException;
066    import javax.xml.transform.Transformer;
067    import javax.xml.transform.TransformerFactory;
068    import javax.xml.transform.dom.DOMSource;
069    import javax.xml.transform.stream.StreamResult;
070    
071    import org.apache.commons.fileupload.FileItem;
072    import org.apache.commons.fileupload.disk.DiskFileItem;
073    import org.apache.commons.fileupload.servlet.ServletFileUpload;
074    
075    import org.w3c.dom.Document;
076    import org.w3c.dom.Element;
077    import org.w3c.dom.Node;
078    
079    /**
080     * @author Ivica Cardic
081     * @author Raymond Aug??
082     */
083    public abstract class BaseCommandReceiver implements CommandReceiver {
084    
085            @Override
086            public void createFolder(
087                    CommandArgument commandArgument, HttpServletRequest request,
088                    HttpServletResponse response) {
089    
090                    Document document = _createDocument();
091    
092                    Node rootNode = _createRoot(
093                            document, commandArgument.getCommand(), commandArgument.getType(),
094                            commandArgument.getCurrentFolder(), StringPool.BLANK);
095    
096                    Element errorElement = document.createElement("Error");
097    
098                    rootNode.appendChild(errorElement);
099    
100                    String returnValue = "0";
101    
102                    try {
103                            returnValue = createFolder(commandArgument);
104                    }
105                    catch (FCKException fcke) {
106                            Throwable cause = fcke.getCause();
107    
108                            returnValue = "110";
109    
110                            if (cause != null) {
111                                    String causeString = GetterUtil.getString(cause.toString());
112    
113                                    if (causeString.contains("DuplicateFolderNameException")) {
114                                            returnValue = "101";
115                                    }
116                                    else if (causeString.contains("FolderNameException")) {
117                                            returnValue = "102";
118                                    }
119                                    else if (causeString.contains("NoSuchGroupException") ||
120                                                     causeString.contains("PrincipalException")) {
121    
122                                            returnValue = "103";
123                                    }
124                                    else {
125                                            throw fcke;
126                                    }
127                            }
128                    }
129    
130                    errorElement.setAttribute("number", returnValue);
131    
132                    _writeDocument(document, response);
133            }
134    
135            @Override
136            public void fileUpload(
137                    CommandArgument commandArgument, HttpServletRequest request,
138                    HttpServletResponse response) {
139    
140                    InputStream inputStream = null;
141    
142                    String returnValue = null;
143    
144                    try {
145                            ServletFileUpload servletFileUpload = new LiferayFileUpload(
146                                    new LiferayFileItemFactory(
147                                            UploadServletRequestImpl.getTempDir()), request);
148    
149                            servletFileUpload.setFileSizeMax(
150                                    PrefsPropsUtil.getLong(
151                                            PropsKeys.UPLOAD_SERVLET_REQUEST_IMPL_MAX_SIZE));
152    
153                            LiferayServletRequest liferayServletRequest =
154                                    new LiferayServletRequest(request);
155    
156                            List<FileItem> fileItems = servletFileUpload.parseRequest(
157                                    liferayServletRequest);
158    
159                            Map<String, Object> fields = new HashMap<String, Object>();
160    
161                            for (FileItem fileItem : fileItems) {
162                                    if (fileItem.isFormField()) {
163                                            fields.put(fileItem.getFieldName(), fileItem.getString());
164                                    }
165                                    else {
166                                            fields.put(fileItem.getFieldName(), fileItem);
167                                    }
168                            }
169    
170                            DiskFileItem diskFileItem = (DiskFileItem)fields.get("NewFile");
171    
172                            String fileName = StringUtil.replace(
173                                    diskFileItem.getName(), CharPool.BACK_SLASH, CharPool.SLASH);
174                            String[] fileNameArray = StringUtil.split(fileName, '/');
175                            fileName = fileNameArray[fileNameArray.length - 1];
176    
177                            String contentType = diskFileItem.getContentType();
178    
179                            if (Validator.isNull(contentType) ||
180                                    contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
181    
182                                    contentType = MimeTypesUtil.getContentType(
183                                            diskFileItem.getStoreLocation());
184                            }
185    
186                            if (diskFileItem.isInMemory()) {
187                                    inputStream = diskFileItem.getInputStream();
188                            }
189                            else {
190                                    inputStream = new ByteArrayFileInputStream(
191                                            diskFileItem.getStoreLocation(),
192                                            LiferayFileItem.THRESHOLD_SIZE);
193                            }
194    
195                            long size = diskFileItem.getSize();
196    
197                            returnValue = fileUpload(
198                                    commandArgument, fileName, inputStream, contentType, size);
199                    }
200                    catch (Exception e) {
201                            FCKException fcke = null;
202    
203                            if (e instanceof FCKException) {
204                                    fcke = (FCKException)e;
205                            }
206                            else {
207                                    fcke = new FCKException(e);
208                            }
209    
210                            Throwable cause = fcke.getCause();
211    
212                            returnValue = "203";
213    
214                            if (cause != null) {
215                                    String causeString = GetterUtil.getString(cause.toString());
216    
217                                    if (causeString.contains("DuplicateFileException")) {
218                                            returnValue = "201";
219                                    }
220                                    else if (causeString.contains("NoSuchFolderException")||
221                                                     causeString.contains("NoSuchGroupException")) {
222    
223                                            returnValue = "204";
224                                    }
225                                    else if (causeString.contains("ImageNameException")) {
226                                            returnValue = "205";
227                                    }
228                                    else if (causeString.contains("FileExtensionException") ||
229                                                     causeString.contains("FileNameException")) {
230    
231                                            returnValue = "206";
232                                    }
233                                    else if (causeString.contains("PrincipalException")) {
234                                            returnValue = "207";
235                                    }
236                                    else if (causeString.contains("FileSizeException") ||
237                                                     causeString.contains("ImageSizeException") ||
238                                                     causeString.contains("SizeLimitExceededException")) {
239    
240                                            returnValue = "208";
241                                    }
242                                    else if (causeString.contains("SystemException")) {
243                                            returnValue = "209";
244                                    }
245                                    else if (causeString.contains("AssetCategoryException")) {
246                                            returnValue = "212";
247                                    }
248                                    else if (causeString.contains("AntivirusScannerException")) {
249                                            returnValue = "211";
250                                    }
251                                    else {
252                                            throw fcke;
253                                    }
254                            }
255    
256                            _writeUploadResponse(returnValue, response);
257                    }
258                    finally {
259                            StreamUtil.cleanUp(inputStream);
260                    }
261    
262                    _writeUploadResponse(returnValue, response);
263            }
264    
265            @Override
266            public void getFolders(
267                    CommandArgument commandArgument, HttpServletRequest request,
268                    HttpServletResponse response) {
269    
270                    Document document = _createDocument();
271    
272                    Node rootNode = _createRoot(
273                            document, commandArgument.getCommand(), commandArgument.getType(),
274                            commandArgument.getCurrentFolder(), getPath(commandArgument));
275    
276                    getFolders(commandArgument, document, rootNode);
277    
278                    _writeDocument(document, response);
279            }
280    
281            @Override
282            public void getFoldersAndFiles(
283                    CommandArgument commandArgument, HttpServletRequest request,
284                    HttpServletResponse response) {
285    
286                    Document document = _createDocument();
287    
288                    Node rootNode = _createRoot(
289                            document, commandArgument.getCommand(), commandArgument.getType(),
290                            commandArgument.getCurrentFolder(), getPath(commandArgument));
291    
292                    getFoldersAndFiles(commandArgument, document, rootNode);
293    
294                    _writeDocument(document, response);
295            }
296    
297            protected abstract String createFolder(CommandArgument commandArgument);
298    
299            protected abstract String fileUpload(
300                    CommandArgument commandArgument, String fileName,
301                    InputStream inputStream, String contentType, long size);
302    
303            protected abstract void getFolders(
304                    CommandArgument commandArgument, Document document, Node rootNode);
305    
306            protected abstract void getFoldersAndFiles(
307                    CommandArgument commandArgument, Document document, Node rootNode);
308    
309            protected String getPath(CommandArgument commandArgument) {
310                    return StringPool.BLANK;
311            }
312    
313            protected void getRootFolders(
314                            CommandArgument commandArgument, Document document,
315                            Element foldersElement)
316                    throws Exception {
317    
318                    List<Group> groups = new UniqueList<Group>();
319    
320                    LinkedHashMap<String, Object> groupParams =
321                            new LinkedHashMap<String, Object>();
322    
323                    groupParams.put("usersGroups", new Long(commandArgument.getUserId()));
324    
325                    groups.addAll(
326                            GroupLocalServiceUtil.search(
327                                    commandArgument.getCompanyId(), null, null, groupParams,
328                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS));
329    
330                    List<Organization> userOrgs =
331                            OrganizationLocalServiceUtil.getUserOrganizations(
332                                    commandArgument.getUserId());
333    
334                    for (Organization organization : userOrgs) {
335                            groups.add(0, organization.getGroup());
336                    }
337    
338                    if (PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_ENABLED ||
339                            PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_ENABLED) {
340    
341                            Group userGroup = GroupLocalServiceUtil.getUserGroup(
342                                    commandArgument.getCompanyId(), commandArgument.getUserId());
343    
344                            groups.add(0, userGroup);
345                    }
346    
347                    Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
348                            commandArgument.getCompanyId());
349    
350                    groups.add(0, companyGroup);
351    
352                    ThemeDisplay themeDisplay = commandArgument.getThemeDisplay();
353    
354                    long doAsGroupId = themeDisplay.getDoAsGroupId();
355    
356                    HttpServletRequest request = commandArgument.getHttpServletRequest();
357    
358                    String portletId = ParamUtil.getString(request, "p_p_id");
359    
360                    for (Group group : groups) {
361                            Element folderElement = document.createElement("Folder");
362    
363                            foldersElement.appendChild(folderElement);
364    
365                            long groupId = group.getGroupId();
366                            String descriptiveName = group.getDescriptiveName();
367    
368                            if (group.hasStagingGroup()) {
369                                    Layout layout = themeDisplay.getLayout();
370    
371                                    Group stagingGroup = group.getStagingGroup();
372    
373                                    if ((layout.isTypeControlPanel() ||
374                                             (stagingGroup.getGroupId() == doAsGroupId)) &&
375                                            group.isStagedPortlet(portletId) &&
376                                            !group.isStagedRemotely() && isStagedData(group)) {
377    
378                                            groupId = stagingGroup.getGroupId();
379                                            descriptiveName = stagingGroup.getDescriptiveName();
380                                    }
381                            }
382    
383                            folderElement.setAttribute(
384                                    "name", groupId + " - " + HtmlUtil.escape(descriptiveName));
385                    }
386            }
387    
388            protected String getSize() {
389                    return getSize(0);
390            }
391    
392            protected String getSize(long size) {
393                    return String.valueOf(Math.ceil(size / 1000));
394            }
395    
396            protected boolean isStagedData(Group group) {
397                    return true;
398            }
399    
400            private Document _createDocument() {
401                    try {
402                            DocumentBuilderFactory documentBuilderFactory =
403                                    SecureXMLFactoryProviderUtil.newDocumentBuilderFactory();
404    
405                            DocumentBuilder documentBuilder =
406                                    documentBuilderFactory.newDocumentBuilder();
407    
408                            return documentBuilder.newDocument();
409                    }
410                    catch (ParserConfigurationException pce) {
411                            throw new FCKException(pce);
412                    }
413            }
414    
415            private Node _createRoot(
416                    Document document, String command, String resourceType, String path,
417                    String url) {
418    
419                    Element rootElement = document.createElement("Connector");
420    
421                    document.appendChild(rootElement);
422    
423                    rootElement.setAttribute("command", command);
424                    rootElement.setAttribute("resourceType", resourceType);
425    
426                    Element currentFolderElement = document.createElement("CurrentFolder");
427    
428                    rootElement.appendChild(currentFolderElement);
429    
430                    currentFolderElement.setAttribute("path", path);
431                    currentFolderElement.setAttribute("url", url);
432    
433                    return rootElement;
434            }
435    
436            private void _writeDocument(
437                    Document document, HttpServletResponse response) {
438    
439                    try {
440                            Element documentElement = document.getDocumentElement();
441    
442                            documentElement.normalize();
443    
444                            TransformerFactory transformerFactory =
445                                    TransformerFactory.newInstance();
446    
447                            Transformer transformer = transformerFactory.newTransformer();
448    
449                            DOMSource domSource = new DOMSource(document);
450    
451                            if (_log.isDebugEnabled()) {
452                                    StreamResult streamResult = new StreamResult(System.out);
453    
454                                    transformer.transform(domSource, streamResult);
455                            }
456    
457                            response.setContentType("text/xml; charset=UTF-8");
458                            response.setHeader("Cache-Control", "no-cache");
459    
460                            PrintWriter printWriter = response.getWriter();
461    
462                            StreamResult streamResult = new StreamResult(printWriter);
463    
464                            transformer.transform(domSource, streamResult);
465    
466                            printWriter.flush();
467                            printWriter.close();
468                    }
469                    catch (Exception e) {
470                            throw new FCKException(e);
471                    }
472            }
473    
474            private void _writeUploadResponse(
475                    String returnValue, HttpServletResponse response) {
476    
477                    try {
478                            StringBundler sb = new StringBundler(7);
479    
480                            String newName = StringPool.BLANK;
481    
482                            sb.append("<script type=\"text/javascript\">");
483                            sb.append("window.parent.frames['frmUpload'].OnUploadCompleted(");
484                            sb.append(returnValue);
485                            sb.append(",'");
486                            sb.append(newName);
487                            sb.append("');");
488                            sb.append("</script>");
489    
490                            response.setContentType("text/html; charset=UTF-8");
491                            response.setHeader("Cache-Control", "no-cache");
492    
493                            PrintWriter printWriter = null;
494    
495                            printWriter = response.getWriter();
496    
497                            printWriter.print(sb.toString());
498    
499                            printWriter.flush();
500                            printWriter.close();
501                    }
502                    catch (Exception e) {
503                            throw new FCKException(e);
504                    }
505            }
506    
507            private static Log _log = LogFactoryUtil.getLog(BaseCommandReceiver.class);
508    
509    }