Debian Codegraph @

util/buildscripts/webbuild/server/src/org/dtk/BuilderServlet.java

raw symbols SCIP
1 package org.dtk;
2
3 import java.io.BufferedInputStream;
4 import java.io.DataInputStream;
5 import java.io.File;
6 import java.io.FileInputStream;
7 import java.io.IOException;
8 import java.io.OutputStream;
9 import java.io.PrintWriter;
10 import java.security.MessageDigest;
11 import java.security.NoSuchAlgorithmException;
12
13 import javax.servlet.ServletContext;
14 import javax.servlet.ServletException;
15 import javax.servlet.http.HttpServlet;
16 import javax.servlet.http.HttpServletRequest;
17 import javax.servlet.http.HttpServletResponse;
18
19 import org.mozilla.javascript.ContextFactory;
20
21 import sun.misc.BASE64Encoder;
22
23 public class BuilderServlet extends HttpServlet {
24 private String builderPath;
25 private String cachePath;
26
27 // *****************************************************
28 public void init() throws ServletException {
29 super.init();
30
31 // Read in the builderPath.
32 // - context-param
33 // - Java system property
34 ServletContext sc = getServletContext();
35 builderPath = sc.getInitParameter("builderPath");
36 cachePath = sc.getInitParameter("cachePath");
37 if (builderPath == null || builderPath.length() == 0) {
38 builderPath = System.getProperty("builderPath");
39 cachePath = System.getProperty("cachePath");
40 }
41 if (cachePath == null || cachePath.length() == 0) {
42 cachePath = System.getProperty("cachePath");
43 }
44 }
45
46 // *****************************************************
47 public void doPost(HttpServletRequest req, HttpServletResponse res)
48 throws ServletException, IOException {
49 doGet(req, res);
50 }
51
52 // *****************************************************
53 public void doGet(HttpServletRequest req, HttpServletResponse res)
54 throws ServletException, IOException {
55
56 //Pull out the parameters.
57 String version = req.getParameter("version");
58 String cdn = req.getParameter("cdn");
59 String dependencies = req.getParameter("dependencies");
60 String optimize = req.getParameter("optimize");
61
62 String cacheFile = null;
63 String result = null;
64 boolean isCached = false;
65
66 Boolean isError = true;
67
68 //Validate parameters
69 if(!version.equals("1.3.2")) {
70 result = "invalid version: " + version;
71 }
72 if(!cdn.equals("google") && !cdn.equals("aol")) {
73 result = "invalide CDN type: " + cdn;
74 }
75 if(!optimize.equals("comments") && !optimize.equals("shrinksafe")
76 && !optimize.equals("none") && !optimize.equals("shrinksafe.keepLines")) {
77 result = "invalid optimize type: " + optimize;
78 }
79 if(!dependencies.matches("^[\\w\\-\\,\\s\\.]+$")) {
80 result = "invalid dependency list: " + dependencies;
81 }
82
83 try {
84 //See if we already did the work.
85 MessageDigest md = null;
86 try {
87 md = MessageDigest.getInstance("SHA");
88 } catch (NoSuchAlgorithmException e) {
89 result = e.getMessage();
90 }
91 if (result == null) {
92 md.update(dependencies.getBytes());
93 String digest = (new BASE64Encoder()).encode(md.digest()).replace('+', '~').replace('/', '_').replace('=', '_');
94 cacheFile = cachePath + "/" + version + "/" + cdn + "/" + digest + "/" + optimize + ".js";
95
96 File file = new File(cacheFile);
97 if (file.exists()) {
98 isCached = true;
99 isError = false;
100 }
101 }
102
103 //Generate the build.
104 if (result == null && !isCached) {
105 BuilderContextAction contextAction = new BuilderContextAction(builderPath, version, cdn, dependencies, optimize);
106 ContextFactory.getGlobal().call(contextAction);
107 Exception exception = contextAction.getException();
108
109 if (exception != null) {
110 result = exception.getMessage();
111 } else {
112 result = contextAction.getResult();
113 FileUtil.writeToFile(cacheFile, result, null, true);
114 isError = false;
115 }
116 }
117 } catch (Exception e) {
118 result = e.getMessage();
119 }
120
121 //Write out response.
122 res.setCharacterEncoding("utf-8");
123 if (isError) {
124 result = result.replaceAll("\\\"", "\\\"");
125 result = "<html><head><script type=\"text/javascript\">alert(\"" + result + "\");</script></head><body></body></html>";
126 PrintWriter writer = res.getWriter();
127 writer.append(result);
128 } else {
129 res.setHeader("Content-Type", "application/x-javascript");
130 res.setHeader("Content-disposition", "attachment; filename=dojo.js");
131 res.setHeader("Content-Encoding", "gzip");
132
133 //Read in the gzipped bytes of the cached file.
134 File file = new File(cacheFile);
135
136 BufferedInputStream in = new java.io.BufferedInputStream(
137 new DataInputStream(new FileInputStream(file)));
138 OutputStream out = res.getOutputStream();
139 byte[] bytes = new byte[64000];
140 int bytesRead = 0;
141 while (bytesRead != -1) {
142 bytesRead = in.read(bytes);
143 if (bytesRead != -1) {
144 out.write(bytes, 0, bytesRead);
145 }
146 }
147 }
148 }
149 }