Package org.xlightweb.test

Source Code of org.xlightweb.test.MultipartTest$Content

/*
*  Copyright (c) xlightweb.org, 2006 - 2009. 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.
*
*  You should have received a copy of the GNU Lesser General Public
*  License along with this library; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
* The latest copy of this software may be found on http://www.xlightweb.org/
*/
package org.xlightweb.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.Map;

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

import org.junit.Assert;
import org.junit.Test;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.xlightweb.IHttpResponse;
import org.xlightweb.MultipartFormDataRequest;
import org.xlightweb.QAUtil;
import org.xlightweb.WebContainer;
import org.xlightweb.client.HttpClient;



/**
*
* @author grro@xlightweb.org
*/
public final class MultipartTest {
 

  @Test
  public void testClientSideUsage() throws Exception {

        WebContainer server = new WebContainer(new TestServlet());
        server.start();
           
        HttpClient httpClient = new HttpClient();
           
        MultipartFormDataRequest req = new MultipartFormDataRequest("http://localhost:" + server.getLocalPort()+ "/");
       
       

       
        String name = "file40k";
        File file = QAUtil.createTestfile_4k();
        req.addPart(name, file);
        req.addPart("test1", "I wake up early and ...");
        req.addPart("test2", "... saw the light");       
       
        IHttpResponse resp = httpClient.call(req);
       
        FileInputStream fis = new FileInputStream(file);
        Content content = new Content(name, file.getName(), "text/html", fis);
        String body = resp.getBlockingBody().toString();
       
        Assert.assertEquals(content.toString() + "\r\n", body);
          
        fis.close();
        file.delete();
        httpClient.close();
        server.stop();
    }


   private static final class TestServlet extends HttpServlet {
   
       private CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
   
       @Override
       protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       
           StringBuilder sb = new StringBuilder();
       
           MultipartHttpServletRequest multipartRequest = multipartResolver.resolveMultipart(req);
       
           Map filemap = multipartRequest.getFileMap();
       
           for (Object name : filemap.keySet()) {
           
               MultipartFile mpf = (MultipartFile) filemap.get(name);
               String orgFilename = mpf.getOriginalFilename();
               String contentType = mpf.getContentType();
               InputStream is = mpf.getInputStream();

               Content content = new Content((String) name, orgFilename, contentType, is);
               sb.append(content + "\r\n");
           }
       
       
           resp.getWriter().write(sb.toString());
       }
   }



   private static final class Content {
   
       private String content;
   
       public Content(String name, String filename, String contentType, InputStream is) throws IOException {
           StringBuilder sb = new StringBuilder();
           sb.append("name=" + name +"\r\n");
           sb.append("filename=" + filename +"\r\n");
           sb.append("contentType=" + contentType +"\r\n");
           sb.append("content=");
       
           LineNumberReader lnr = new LineNumberReader(new InputStreamReader(is));
           String line = null;
           try {
           do {
               line = lnr.readLine();
               if (line != null)  {
                   sb.append(line + "\r\n");
               }
           } while (line != null);
           } catch (IOException ioe) {
               ioe.printStackTrace();
           }
       
           content = sb.toString();
       }
   
   
       @Override
       public String toString() {
           return content;
       }
   }
}
TOP

Related Classes of org.xlightweb.test.MultipartTest$Content

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.