Skip to content

Commit 783ef4c

Browse files
committed
Refactoring
1 parent 4989b80 commit 783ef4c

File tree

11 files changed

+390
-144
lines changed

11 files changed

+390
-144
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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;
26+
27+
public class BioJavaRoutes {
28+
public static String PDB = "/pdb/:id";
29+
public static String MMCIF = "/mmcif/:id";
30+
public static String NGL = "/ngl/:id";
31+
}

src/main/java/org/biojava/http/ServerMain.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,22 @@
2626

2727
import static spark.Spark.*;
2828

29+
import org.biojava.http.routes.MMCIFRoute;
30+
import org.biojava.http.routes.NGLRoute;
31+
import org.biojava.http.routes.PDBRoute;
32+
33+
import spark.template.handlebars.HandlebarsTemplateEngine;
34+
2935
public class ServerMain {
3036
public static void main(String[] args) {
31-
// port(5678); <- Uncomment this if you want spark to listen to port 5678 in stead of the default 4567
32-
get("/hello", (request, response) -> "Hello World!");
37+
// port(4567);
38+
39+
get("/", (r,r2) -> "BioJava HTTP");
40+
41+
get(BioJavaRoutes.PDB, new PDBRoute());
42+
43+
get(BioJavaRoutes.MMCIF, new MMCIFRoute());
44+
45+
get(BioJavaRoutes.NGL, new NGLRoute(), new HandlebarsTemplateEngine());
3346
}
3447
}

src/main/java/org/biojava/http/structure/CESymmParams.java renamed to src/main/java/org/biojava/http/models/CESymmParams.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*
2323
*/
2424

25-
package org.biojava.http.structure;
25+
package org.biojava.http.models;
2626

2727
import org.biojava.nbio.structure.symmetry.internal.CeSymmResult;
2828

src/main/java/org/biojava/http/structure/NGLParams.java renamed to src/main/java/org/biojava/http/models/NGLParams.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*
2323
*/
2424

25-
package org.biojava.http.structure;
25+
package org.biojava.http.models;
2626

2727
public class NGLParams {
2828
private String url;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.models;
26+
27+
28+
public class NGLRouteParams extends NGLParams{
29+
private String structureId;
30+
31+
public NGLRouteParams(String structureId,String structUrl) {
32+
super(structUrl);
33+
this.structureId = structureId;
34+
}
35+
36+
public String getStructureId() {
37+
return structureId;
38+
}
39+
40+
public void setStructureId(String structureId) {
41+
this.structureId = structureId;
42+
}
43+
44+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 org.biojava.http.BioJavaRoutes;
28+
import org.biojava.http.models.CESymmParams;
29+
import org.biojava.nbio.structure.Atom;
30+
import org.biojava.nbio.structure.Structure;
31+
import org.biojava.nbio.structure.StructureIO;
32+
import org.biojava.nbio.structure.StructureTools;
33+
import org.biojava.nbio.structure.symmetry.internal.CeSymm;
34+
import org.biojava.nbio.structure.symmetry.internal.CeSymmResult;
35+
import org.slf4j.Logger;
36+
import org.slf4j.LoggerFactory;
37+
38+
import spark.ModelAndView;
39+
import spark.Request;
40+
import spark.Response;
41+
import spark.TemplateViewRoute;
42+
43+
/**
44+
* Handle requests for {@link BioJavaRoutes#MMCIF}
45+
* @author Spencer Bliven
46+
*
47+
*/
48+
public class CESymmRoute implements TemplateViewRoute {
49+
public static Logger logger = LoggerFactory.getLogger(CESymmRoute.class);
50+
51+
@Override
52+
public ModelAndView handle(Request request, Response response) throws Exception {
53+
String id = request.params(":id");
54+
if(id == null) {
55+
response.status(404);
56+
logger.error("null id");
57+
return null;
58+
}
59+
try {
60+
Structure s = StructureIO.getStructure(id);
61+
if(s == null) {
62+
response.status(404);
63+
logger.error("null structure for "+id);
64+
return null;
65+
}
66+
Atom[] ca = StructureTools.getRepresentativeAtomArray(s);
67+
CeSymmResult result = CeSymm.analyze(ca);
68+
69+
CESymmParams params = new CESymmParams(result);
70+
return new ModelAndView(params, "cesymm.html.hbs");
71+
} catch(Exception e) {
72+
logger.error("Error",e);
73+
response.status(404);
74+
return null;
75+
}
76+
}
77+
78+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 org.biojava.http.BioJavaRoutes;
28+
import org.biojava.nbio.structure.Structure;
29+
import org.biojava.nbio.structure.StructureException;
30+
import org.biojava.nbio.structure.StructureIO;
31+
import org.slf4j.Logger;
32+
import org.slf4j.LoggerFactory;
33+
34+
import spark.Request;
35+
import spark.Response;
36+
import spark.Route;
37+
38+
/**
39+
* Handle requests for {@link BioJavaRoutes#MMCIF}
40+
* @author Spencer Bliven
41+
*
42+
*/
43+
public class MMCIFRoute implements Route {
44+
public static Logger logger = LoggerFactory.getLogger(MMCIFRoute.class);
45+
46+
@Override
47+
public String handle(Request request, Response response) throws Exception {
48+
String id = request.params(":id");
49+
if(id == null) {
50+
response.status(404);
51+
return "No structure specified";
52+
}
53+
try {
54+
Structure s = StructureIO.getStructure(id);
55+
if(s == null) {
56+
response.status(404);
57+
return "Error fetching "+id;
58+
}
59+
String pdb = s.toMMCIF();
60+
String filename = id+".cif";
61+
response.header("Content-Type", "chemical/x-pdb");
62+
response.header("Content-Disposition", String.format("inline; filename=\"%s\"",filename));
63+
return pdb;
64+
} catch(StructureException e) {
65+
logger.error("Error",e);
66+
response.status(404);
67+
return "Error fetching "+id;
68+
}
69+
}
70+
71+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 org.biojava.http.BioJavaRoutes;
28+
import org.biojava.http.models.NGLRouteParams;
29+
import org.slf4j.Logger;
30+
import org.slf4j.LoggerFactory;
31+
32+
import spark.ModelAndView;
33+
import spark.Request;
34+
import spark.Response;
35+
import spark.TemplateViewRoute;
36+
37+
/**
38+
* Handle requests for {@link BioJavaRoutes#MMCIF}
39+
* @author Spencer Bliven
40+
*
41+
*/
42+
public class NGLRoute implements TemplateViewRoute {
43+
public static Logger logger = LoggerFactory.getLogger(NGLRoute.class);
44+
45+
@Override
46+
public ModelAndView handle(Request request, Response response) throws Exception {
47+
String id = request.params(":id");
48+
if(id == null) {
49+
response.status(404);
50+
logger.error("null id");
51+
return null;
52+
}
53+
try {
54+
String structUrl = BioJavaRoutes.MMCIF.replace(":id", id);
55+
NGLRouteParams params = new NGLRouteParams(id,structUrl);
56+
return new ModelAndView(params, "ngl.html.hbs");
57+
} catch(Exception e) {
58+
logger.error("Error",e);
59+
response.status(404);
60+
return null;
61+
}
62+
}
63+
64+
}

0 commit comments

Comments
 (0)