|
| 1 | +/* |
| 2 | + * BioJava development code |
| 3 | + * |
| 4 | + * This code may be freely distributed and modified under the |
| 5 | + * terms of the GNU Lesser General Public Licence. This should |
| 6 | + * be distributed with the code. If you do not have a copy, |
| 7 | + * see: |
| 8 | + * |
| 9 | + * http://www.gnu.org/copyleft/lesser.html |
| 10 | + * |
| 11 | + * Copyright for this code is held jointly by the individual |
| 12 | + * authors. These should be listed in @author doc comments. |
| 13 | + * |
| 14 | + * For more information on the BioJava project and its aims, |
| 15 | + * or to join the biojava-l mailing list, visit the home page |
| 16 | + * at: |
| 17 | + * |
| 18 | + * http://www.biojava.org/ |
| 19 | + * |
| 20 | + * Created on Jul 6, 2016 |
| 21 | + * Author: blivens |
| 22 | + * |
| 23 | + */ |
| 24 | + |
| 25 | +package org.biojava.http.routes; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | + |
| 29 | +import javax.servlet.ServletOutputStream; |
| 30 | + |
| 31 | +import org.biojava.http.BioJavaRoutes; |
| 32 | +import org.biojava.nbio.structure.Structure; |
| 33 | +import org.biojava.nbio.structure.StructureException; |
| 34 | +import org.biojava.nbio.structure.StructureIO; |
| 35 | +import org.biojava.nbio.structure.io.mmtf.MmtfActions; |
| 36 | +import org.slf4j.Logger; |
| 37 | +import org.slf4j.LoggerFactory; |
| 38 | + |
| 39 | +import spark.Request; |
| 40 | +import spark.Response; |
| 41 | +import spark.Route; |
| 42 | +import spark.Spark; |
| 43 | + |
| 44 | +/** |
| 45 | + * Handle requests for {@link BioJavaRoutes#MMTF} |
| 46 | + * @author Spencer Bliven |
| 47 | + * |
| 48 | + */ |
| 49 | +public class MMTFRoute implements Route { |
| 50 | + public static Logger logger = LoggerFactory.getLogger(MMTFRoute.class); |
| 51 | + |
| 52 | + @Override |
| 53 | + public String handle(Request request, Response response) { |
| 54 | + String id = request.params(":id"); |
| 55 | + if(id == null) { |
| 56 | + Spark.halt(404, "No structure specified" ); |
| 57 | + return null; |
| 58 | + } |
| 59 | + try { |
| 60 | + Structure s = StructureIO.getStructure(id); |
| 61 | + if(s == null) { |
| 62 | + response.status(404); |
| 63 | + return "Error fetching "+request; |
| 64 | + } |
| 65 | + handleStructure(s, response); |
| 66 | + return "200 OK"; // body is set explicitly |
| 67 | + } catch(StructureException | IOException e) { |
| 68 | + logger.error("Error",e); |
| 69 | + Spark.halt(404,"Error fetching "+id); |
| 70 | + return null; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public static void handleStructure( Structure s, Response response) throws IOException { |
| 75 | + ServletOutputStream out = response.raw().getOutputStream(); |
| 76 | + MmtfActions.writeToOutputStream(s,out); |
| 77 | + response.type( "application/octet-stream"); |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | +} |
0 commit comments