Team:Freiburg software/Code/AbstractSynBioWaveServlet.java

From 2009.igem.org

Revision as of 22:17, 20 October 2009 by Paul.S (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


org.synbiowave.servlet.AbstractSynBioWaveRobot

/*
    Copyright: synbiowave
     
    License: GPL
     
    Authors: Jörg Wassolesk
     
    Version: 0.1 
    
    DESCRIPTION:
    	This class provides the functions for recieving url-connections.
    	
    	Our central SynBioWave-Robot extends this class in instead of AbstractSbwRobot.
    	All others don't.
*/


package org.synbiowave.servlet;

import java.io.BufferedInputStream;

@SuppressWarnings("serial")
public abstract class AbstractSynBioWaveServlet extends AbstractRobotServlet{
	
	protected String plain=null;
	protected String upload=null;
	protected String download=null;

	@Override
	protected void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException {
		if(req.getContentType().equals("application/json; charset=UTF-8")){super.doPost(req,resp);}
		else if(req.getContentType().contains("multipart/form-data")){
			try{
				ServletFileUpload fileUpload=new ServletFileUpload();
				resp.setContentType("text/html");
				PrintWriter out=resp.getWriter();
				try{
					FileItemIterator iterator=fileUpload.getItemIterator(req);
					while(iterator.hasNext()){
						FileItemStream item=iterator.next();
						InputStream in=item.openStream();
						if(!item.isFormField()){
							try{
								plain=IOUtils.toString(in);
								try{
									upload=Util.read(new ByteArrayInputStream(plain.getBytes())).nextRichSequence().seqString();
									out.println("<div style=\"border:2px solid black;font-family:monospace;white-space:nowrap;overflow:auto;\">"+SequenceView.getHTMLView(upload,SequenceView.isSequence(upload))+"<br /></div>");}
								catch(Exception exc){upload=exc.toString();}}
							finally{IOUtils.closeQuietly(in);}}}} 
				catch(SizeLimitExceededException e){upload=e.toString();}} 
			catch(Exception ex){upload=ex.toString();}}
		else if(req.getContentType().contains("application/x-www-form-urlencoded")){
			try{
				PrintWriter out=resp.getWriter();
		        resp.setContentType("text/plain");
		        resp.setContentLength((int)download.getBytes().length);
		        resp.setHeader("Content-Disposition","attachment;filename=\"TestFile.txt\"");
				out.print(download);}
			catch (Exception ex) {}}
		else if(req.getContentType().contains("application/x-java-serialized-object")){
			ArrayList<Object> request=new ArrayList<Object>();
			try{
				ObjectInputStream in=new ObjectInputStream(new BufferedInputStream(req.getInputStream()));
				request.add(in.readObject());
				request.add(in.readObject());
				in.close();
				
				byte[] arr=(byte[])request.get(0);
				String s=(String)request.get(1);

				ObjectOutputStream out=new ObjectOutputStream(new BufferedOutputStream(resp.getOutputStream()));
				out.writeObject(arr);
				out.writeObject(s);
				out.close();}
			catch(Exception e){}}}
}