com.liferay.blogs.web.internal.upload.ImageBlogsUploadResponseHandler.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.blogs.web.internal.upload.ImageBlogsUploadResponseHandler.java

Source

/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library 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 library 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.
 */

package com.liferay.blogs.web.internal.upload;

import com.liferay.blogs.exception.EntryImageNameException;
import com.liferay.blogs.exception.EntryImageSizeException;
import com.liferay.item.selector.ItemSelectorUploadResponseHandler;
import com.liferay.petra.string.StringPool;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.repository.model.FileEntry;
import com.liferay.portal.kernel.servlet.ServletResponseConstants;
import com.liferay.portal.kernel.upload.UploadPortletRequest;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.util.PrefsPropsUtil;
import com.liferay.upload.UploadResponseHandler;

import javax.portlet.PortletRequest;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
 * @author Alejandro Tardn
 */
@Component(service = ImageBlogsUploadResponseHandler.class)
public class ImageBlogsUploadResponseHandler implements UploadResponseHandler {

    @Override
    public JSONObject onFailure(PortletRequest portletRequest, PortalException pe) throws PortalException {

        JSONObject jsonObject = _itemSelectorUploadResponseHandler.onFailure(portletRequest, pe);

        if (pe instanceof EntryImageNameException || pe instanceof EntryImageSizeException) {

            String errorMessage = StringPool.BLANK;
            int errorType = 0;

            if (pe instanceof EntryImageNameException) {
                errorType = ServletResponseConstants.SC_FILE_EXTENSION_EXCEPTION;

                String[] imageExtensions = PrefsPropsUtil.getStringArray(PropsKeys.BLOGS_IMAGE_EXTENSIONS,
                        StringPool.COMMA);

                errorMessage = StringUtil.merge(imageExtensions);
            } else if (pe instanceof EntryImageSizeException) {
                errorType = ServletResponseConstants.SC_FILE_SIZE_EXCEPTION;
            }

            JSONObject errorJSONObject = JSONFactoryUtil.createJSONObject();

            errorJSONObject.put("errorType", errorType);
            errorJSONObject.put("message", errorMessage);

            jsonObject.put("error", errorJSONObject);
        }

        return jsonObject;
    }

    @Override
    public JSONObject onSuccess(UploadPortletRequest uploadPortletRequest, FileEntry fileEntry)
            throws PortalException {

        return _itemSelectorUploadResponseHandler.onSuccess(uploadPortletRequest, fileEntry);
    }

    @Reference
    private ItemSelectorUploadResponseHandler _itemSelectorUploadResponseHandler;

}