org.jasig.portlet.test.mvc.tests.FileUploadController.java Source code

Java tutorial

Introduction

Here is the source code for org.jasig.portlet.test.mvc.tests.FileUploadController.java

Source

/**
 * Licensed to Jasig under one or more contributor license
 * agreements. See the NOTICE file distributed with this work
 * for additional information regarding copyright ownership.
 * Jasig licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a
 * copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.jasig.portlet.test.mvc.tests;

import java.io.IOException;
import java.util.Map;

import javax.portlet.PortletSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;

import com.google.common.collect.ImmutableMap;

/**
 * @author Eric Dalquist
 * @version $Revision$
 */
@Controller("fileUploadTest")
@RequestMapping(value = { "VIEW", "EDIT", "HELP", "ABOUT" }, params = "currentTest=fileUploadTest")
public class FileUploadController extends BasePortletTest {
    private static final String UPLOADED_FILE_INFO = FileUploadController.class.getName() + ".UPLOADED_FILE_INFO";

    @Override
    public String getTestName() {
        return "File Upload Test";
    }

    @RenderMapping
    public String viewFileUploadTest(PortletSession portletSession, ModelMap model) {
        logger.debug("Rendering File Upload Test");

        final Map<String, ? extends Object> fileInfo = (Map<String, ? extends Object>) portletSession
                .getAttribute(UPLOADED_FILE_INFO);
        model.put("fileInfo", fileInfo);

        return "fileUploadTest";
    }

    @ActionMapping
    public void noopAction() {
    }

    @ActionMapping("fileUploadAction")
    public void simpleFileUpload(PortletSession portletSession, @RequestParam("file") MultipartFile f)
            throws IOException {
        final Map<String, ? extends Object> fileInfo = ImmutableMap.of("size", f.getSize(), "originalFilename",
                f.getOriginalFilename(), "name", f.getName(), "contentType", f.getContentType(), "byteCount",
                f.getBytes().length);

        portletSession.setAttribute(UPLOADED_FILE_INFO, fileInfo);
    }
}