Team:Freiburg software/Code/AbstractSynBioWaveServlet.java

From 2009.igem.org

(Difference between revisions)
(New page: <div style="display: none;"><html><style> #tabs a.code_active { background-position:0% -42px; } #tabs a.code_active span { background-position:100% -42px; color:#213340; } #toc { display:n...)
(org.synbiowave.servlet.AbstractSynBioWaveRobot)
Line 33: Line 33:
     All others don't.
     All others don't.
*/
*/
-
 
package org.synbiowave.servlet;
package org.synbiowave.servlet;
-
import java.io.BufferedInputStream;
+
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")
@SuppressWarnings("serial")
public abstract class AbstractSynBioWaveServlet extends AbstractRobotServlet{
public abstract class AbstractSynBioWaveServlet extends AbstractRobotServlet{
 +
 +
/** The current sequence. */
 +
protected Long currentSequence=null;
-
protected String plain=null;
+
/** The format. */
-
protected String upload=null;
+
protected String format="genbank";
-
protected String download=null;
+
 +
/* (non-Javadoc)
 +
* @see com.google.wave.api.AbstractRobotServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 +
*/
@Override
@Override
protected void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException {
protected void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException {
Line 53: Line 87:
ServletFileUpload fileUpload=new ServletFileUpload();
ServletFileUpload fileUpload=new ServletFileUpload();
resp.setContentType("text/html");
resp.setContentType("text/html");
-
PrintWriter out=resp.getWriter();
 
try{
try{
FileItemIterator iterator=fileUpload.getItemIterator(req);
FileItemIterator iterator=fileUpload.getItemIterator(req);
Line 61: Line 94:
if(!item.isFormField()){
if(!item.isFormField()){
try{
try{
-
plain=IOUtils.toString(in);
+
String plain=IOUtils.toString(in);
try{
try{
-
upload=Util.read(new ByteArrayInputStream(plain.getBytes())).nextRichSequence().seqString();
+
RichSequenceIterator rsi=
-
out.println("<div style=\"border:2px solid black;font-family:monospace;white-space:nowrap;overflow:auto;\">"+SequenceView.getHTMLView(upload,SequenceView.isSequence(upload))+"<br /></div>");}
+
                                                                                Util.read(new ByteArrayInputStream(plain.getBytes()));
-
catch(Exception exc){upload=exc.toString();}}
+
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);}}}}  
finally{IOUtils.closeQuietly(in);}}}}  
-
catch(SizeLimitExceededException e){upload=e.toString();}}  
+
catch(SizeLimitExceededException e){}}  
-
catch(Exception ex){upload=ex.toString();}}
+
catch(Exception ex){}}
else if(req.getContentType().contains("application/x-www-form-urlencoded")){
else if(req.getContentType().contains("application/x-www-form-urlencoded")){
try{
try{
-
PrintWriter out=resp.getWriter();
+
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.setContentType("text/plain");
-
        resp.setContentLength((int)download.getBytes().length);
+
        resp.setHeader("Content-Disposition","attachment;filename=\""+sequence.getName()+"."+extension+"\"");
-
        resp.setHeader("Content-Disposition","attachment;filename=\"TestFile.txt\"");
+
Util.write(resp.getOutputStream(),rsdb.getRichSequenceIterator(),format);}
-
out.print(download);}
+
catch (Exception ex) {}}}
-
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){}}}
+
}
}
  </nowiki>
  </nowiki>

Revision as of 23:40, 21 October 2009


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.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) {}}}
}