org.openxdata.server.servlet.FormOpenServlet.java Source code

Java tutorial

Introduction

Here is the source code for org.openxdata.server.servlet.FormOpenServlet.java

Source

/*
 *  Licensed to the OpenXdata Foundation (OXDF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The OXDF 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.
 *
 *  Copyright 2010 http://www.openxdata.org.
 */
package org.openxdata.server.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.openxdata.server.OpenXDataConstants;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

/**
 * Servlet that handles opening of files.
 * 
 * @author daniel
 *
 */
public class FormOpenServlet extends HttpServlet {

    public static final long serialVersionUID = 111111111111113L;

    private static final String KEY_FILE_CONTENTS = "FileContents";

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", -1);
        response.setHeader("Cache-Control", "no-store");

        response.setContentType(OpenXDataConstants.HTTP_HEADER_CONTENT_TYPE_XML);
        response.getOutputStream().print((String) request.getSession().getAttribute(KEY_FILE_CONTENTS));
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            CommonsMultipartResolver multipartResover = new CommonsMultipartResolver(/*this.getServletContext()*/);
            if (multipartResover.isMultipart(request)) {
                MultipartHttpServletRequest multipartRequest = multipartResover.resolveMultipart(request);
                MultipartFile uploadedFile = multipartRequest.getFile("filecontents");
                if (uploadedFile != null && !uploadedFile.isEmpty())
                    request.getSession().setAttribute(KEY_FILE_CONTENTS,
                            IOUtils.toString(uploadedFile.getInputStream(), "UTF-8"));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}