Team:Freiburg software/Code/AbstractSynBioWaveServlet.java

From 2009.igem.org


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. This class enhances the servlet capabilities of the Google Wave Robot Servlet to serve also file up - and
        download.
*/

package org.synbiowave.servlet;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Iterator;

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

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;
import org.biojava.bio.seq.Feature;
import org.biojavax.bio.db.HashRichSequenceDB;
import org.biojavax.bio.db.RichSequenceDB;
import org.biojavax.bio.seq.RichSequence;
import org.biojavax.bio.seq.RichSequenceIterator;
import org.synbiowave.biojava.Util;
import org.synbiowave.database.DatastoreManager;
import org.synbiowave.database.Sequence;
import org.synbiowave.sequence.SequenceView;

import com.google.wave.api.AbstractRobotServlet;
import com.google.wave.api.Annotation;
import com.google.wave.api.Range;

// TODO: Auto-generated Javadoc
/**
 * The Class AbstractSynBioWaveServlet.
 */
@SuppressWarnings("serial")
public abstract class AbstractSynBioWaveServlet extends AbstractRobotServlet{

	/** The current sequence. */
	protected Long currentSequence=null;
	
	/** The format. */
	protected String format="genbank";

	/* (non-Javadoc)
	 * @see com.google.wave.api.AbstractRobotServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@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");
				try{
					FileItemIterator iterator=fileUpload.getItemIterator(req);
					while(iterator.hasNext()){
						FileItemStream item=iterator.next();
						InputStream in=item.openStream();
						if(!item.isFormField()){
							try{
								String plain=IOUtils.toString(in);
								try{
									RichSequenceIterator rsi=
                                                                                 Util.read(new ByteArrayInputStream(plain.getBytes()));
									boolean single=true;
									RichSequence rs;
									while(rsi.hasNext()){
										rs=rsi.nextRichSequence();
										String type;
										if(rs.getAlphabet().getName()!=null){
                                                                                          type=rs.getAlphabet().getName();}
										else{type=SequenceView.isSequence(rs.seqString());}
										Sequence sequence= 
                                                                                          new Sequence("import",
                                                                                                   System.currentTimeMillis(),
                                                                                                   null,null,null,rs.seqString(),type);
										if(rs.getDescription()!=null
                                                                                           &&rs.getDescription().length()>1){
                                                                                                    sequence.setName
                                                                                                    (rs.getDescription());}
										else{
                                                                                           sequence.setName("Import"+"("+
                                                                                           new Date
                                                                                          (System.currentTimeMillis()).toString()
                                                                                            +")");}
										if(rsi.hasNext()){
											Iterator<Feature> fi=rs.features();
											while(fi.hasNext()){
												Feature f=fi.next();
												sequence.addAnnotation(
                                                                                                new Annotation("feature",f.getType(),
                                                                                                new Range(f.getLocation().getMin(),
                                                                                                f.getLocation().getMax())));}}
										else if(single){sequence.setPlain(plain);}
										single=false;
										currentSequence=
                                                                                          DatastoreManager.store(sequence).getId();}}
								catch(Exception exc){}}
							finally{IOUtils.closeQuietly(in);}}}} 
				catch(SizeLimitExceededException e){}} 
			catch(Exception ex){}}
		else if(req.getContentType().contains("application/x-www-form-urlencoded")){
			try{
				Sequence sequence=DatastoreManager.getByID(new Sequence(),currentSequence);
				org.biojava.bio.seq.Sequence s = Util.convertToRichSequence(sequence);
				RichSequenceDB rsdb=new HashRichSequenceDB();
				rsdb.addSequence(s);
				String extension;
				if(format.equals("genbank")){
					if(sequence.getType().equals("PROTEIN")){extension="gp";}
					else{extension="gb";}}
				else{extension=format;}
		        resp.setContentType("text/plain");
		        resp.setHeader("Content-Disposition","attachment;filename=\""+sequence.getName()+"."+extension+"\"");
				Util.write(resp.getOutputStream(),rsdb.getRichSequenceIterator(),format);}
			catch (Exception ex) {}}}
}