001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU General
007: * Public License Version 2 only ("GPL") or the Common Development and Distribution
008: * License("CDDL") (collectively, the "License"). You may not use this file except in
009: * compliance with the License. You can obtain a copy of the License at
010: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
011: * License for the specific language governing permissions and limitations under the
012: * License. When distributing the software, include this License Header Notice in
013: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
014: * designates this particular file as subject to the "Classpath" exception as
015: * provided by Sun in the GPL Version 2 section of the License file that
016: * accompanied this code. If applicable, add the following below the License Header,
017: * with the fields enclosed by brackets [] replaced by your own identifying
018: * information: "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Contributor(s):
021: *
022: * The Original Software is NetBeans. The Initial Developer of the Original Software
023: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
024: * Rights Reserved.
025: *
026: * If you wish your version of this file to be governed by only the CDDL or only the
027: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
028: * this software in this distribution under the [CDDL or GPL Version 2] license." If
029: * you do not indicate a single choice of license, a recipient has the option to
030: * distribute your version of this file under either the CDDL, the GPL Version 2 or
031: * to extend the choice of license to its licensees as provided above. However, if
032: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
033: * the option applies only if the new code is made subject to such option by the
034: * copyright holder.
035: */
036:
037: package org.netbeans.installer.infra.server.client.servlets;
038:
039: import java.io.File;
040: import java.io.IOException;
041: import java.io.OutputStream;
042: import java.io.PrintWriter;
043: import java.net.URLEncoder;
044: import java.util.Date;
045: import java.util.List;
046: import javax.ejb.EJB;
047: import javax.servlet.ServletException;
048: import javax.servlet.http.HttpServlet;
049: import javax.servlet.http.HttpServletRequest;
050: import javax.servlet.http.HttpServletResponse;
051: import org.netbeans.installer.infra.server.ejb.Manager;
052: import org.netbeans.installer.infra.server.ejb.ManagerException;
053: import org.netbeans.installer.product.components.Product;
054: import org.netbeans.installer.product.components.Group;
055: import org.netbeans.installer.product.RegistryNode;
056: import org.netbeans.installer.utils.StringUtils;
057: import org.netbeans.installer.utils.SystemUtils;
058: import org.netbeans.installer.utils.exceptions.ParseException;
059: import org.netbeans.installer.utils.helper.Platform;
060:
061: import static org.netbeans.installer.utils.helper.Platform.WINDOWS;
062: import static org.netbeans.installer.utils.helper.Platform.LINUX;
063: import static org.netbeans.installer.utils.helper.Platform.SOLARIS_X86;
064: import static org.netbeans.installer.utils.helper.Platform.SOLARIS_SPARC;
065: import static org.netbeans.installer.utils.helper.Platform.MACOSX;
066: import static org.netbeans.installer.utils.helper.Platform.MACOSX_X86;
067: import static org.netbeans.installer.utils.helper.Platform.MACOSX_PPC;
068:
069: /**
070: *
071: * @author Kirill Sorokin
072: * @version
073: */
074: public class CreateBundle extends HttpServlet {
075: @EJB
076: private Manager manager;
077:
078: protected void doGet(HttpServletRequest request,
079: HttpServletResponse response) throws ServletException,
080: IOException {
081: if (request.getParameterValues("component") != null) {
082: doPost(request, response);
083: return;
084: }
085:
086: response.setContentType("text/html; charset=UTF-8");
087:
088: String[] registries = request.getParameterValues("registry");
089:
090: PrintWriter out = response.getWriter();
091:
092: try {
093: String userAgent = request.getHeader("User-Agent");
094:
095: Platform platform = SystemUtils.getCurrentPlatform();
096: if (userAgent.contains("Windows")) {
097: platform = WINDOWS;
098: }
099: if (userAgent.contains("PPC Mac OS")) {
100: platform = MACOSX_PPC;
101: }
102: if (userAgent.contains("Intel Mac OS")) {
103: platform = MACOSX_X86;
104: }
105: if (userAgent.contains("Linux")) {
106: platform = LINUX;
107: }
108: if (userAgent.contains("SunOS i86pc")) {
109: platform = SOLARIS_X86;
110: }
111: if (userAgent.contains("SunOS sun4u")) {
112: platform = SOLARIS_SPARC;
113: }
114:
115: if (request.getParameter("platform") != null) {
116: try {
117: platform = StringUtils.parsePlatform(request
118: .getParameter("platform"));
119: } catch (ParseException e) {
120: e.printStackTrace(out);
121: }
122: }
123:
124: out
125: .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">");
126: out.println("<html>");
127: out.println(" <head>");
128: out
129: .println(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>");
130: out.println(" <title>Registries Manager</title>");
131: out
132: .println(" <link rel=\"stylesheet\" href=\"admin/css/main.css\" type=\"text/css\"/>");
133: out
134: .println(" <script src=\"js/main.js\" type=\"text/javascript\"></script>");
135: out.println(" </head>");
136: out.println(" <body>");
137: out.println(" <p>");
138: out
139: .println(" Select the components that you would like to include in the bundle and click Next.");
140: out.println(" </p>");
141: out
142: .println(" <form name=\"Form\" action=\"create-bundle\" method=\"post\">");
143:
144: String registriesUrl = "";
145:
146: for (String registry : registries) {
147: registriesUrl += "®istry="
148: + URLEncoder.encode(registry, "UTF-8");
149: out
150: .println(" <input type=\"hidden\" name=\"registry\" value=\""
151: + registry + "\"/>");
152: }
153: out
154: .println(" <input type=\"hidden\" name=\"registries\" value=\""
155: + registriesUrl + "\"/>");
156: out
157: .println(" <input type=\"hidden\" name=\"platform\" value=\""
158: + platform + "\"/>");
159:
160: out
161: .println(" <select id=\"platforms-select\" onchange=\"update_target_platform()\">");
162: for (Platform temp : Platform.values()) {
163: out
164: .println(" <option value=\""
165: + temp.getCodeName()
166: + "\""
167: + (temp.isCompatibleWith(platform) ? " selected"
168: : "") + ">"
169: + temp.getDisplayName() + "</option>");
170: }
171: out.println(" </select>");
172:
173: out.println(" <div class=\"registry\">");
174: buildRegistryTable(out, manager.loadRegistry(registries)
175: .getRegistryRoot(), platform);
176: out.println(" </div>");
177:
178: out
179: .println(" <input type=\"submit\" value=\"Create Bundle\"/>");
180:
181: out.println(" </form>");
182: out.println(" <br/>");
183: out.println(" <p class=\"small\">" + userAgent
184: + "</p>");
185: out.println(" </body>");
186: out.println("</html>");
187: } catch (ManagerException e) {
188: response.setStatus(HttpServletResponse.SC_NOT_FOUND);
189: e.printStackTrace(out);
190: }
191:
192: out.close();
193: }
194:
195: protected void doPost(HttpServletRequest request,
196: HttpServletResponse response) throws ServletException,
197: IOException {
198: try {
199: final String[] registries = request
200: .getParameterValues("registry");
201: final String[] components = request
202: .getParameterValues("component");
203: final Platform platform = StringUtils.parsePlatform(request
204: .getParameter("platform"));
205:
206: final File file = manager.createBundle(platform,
207: registries, components);
208:
209: String filename = "";
210: for (String name : components) {
211: filename += name.substring(0, name.indexOf(",")) + "_";
212: }
213: filename += platform.toString();
214: if (platform == WINDOWS) {
215: filename += ".exe";
216: } else if (platform.isCompatibleWith(MACOSX)) {
217: filename += ".zip";
218: } else {
219: filename += ".sh";
220: }
221:
222: final OutputStream output = response.getOutputStream();
223:
224: response.setContentType("application/octet-stream");
225: response.setHeader("Content-Disposition",
226: "attachment; filename=" + filename);
227: response.setHeader("Last-Modified", StringUtils
228: .httpFormat(new Date(file.lastModified())));
229: response.setHeader("Accept-Ranges", "bytes");
230:
231: Utils.transfer(request, response, output, file);
232: } catch (ParseException e) {
233: e.printStackTrace(response.getWriter());
234: } catch (ManagerException e) {
235: e.printStackTrace(response.getWriter());
236: }
237: }
238:
239: private void buildRegistryTable(PrintWriter out, RegistryNode root,
240: Platform platform) {
241: out.println(" <table class=\"registry\">");
242:
243: buildRegistryNodes(out, root.getChildren(), platform);
244:
245: out.println(" </table>");
246: }
247:
248: private void buildRegistryNodes(PrintWriter out,
249: List<RegistryNode> nodes, Platform platform) {
250: for (RegistryNode node : nodes) {
251: if (node instanceof Product) {
252: if (!((Product) node).getPlatforms().contains(platform)) {
253: continue;
254: }
255: }
256:
257: String icon = null;
258: String displayName = node.getDisplayName();
259: String treeHandle = null;
260:
261: if (node.getIconUri() == null) {
262: icon = "img/default-icon.png";
263: } else {
264: icon = node.getIconUri().getRemote().toString();
265: }
266:
267: if (node.getChildren().size() > 0) {
268: treeHandle = "img/tree-handle-open.png";
269: } else {
270: treeHandle = "img/tree-handle-empty.png";
271: }
272:
273: String id = null;
274:
275: String uid = node.getUid();
276: String version = null;
277: String type = null;
278: String platforms = null;
279: String title = "";
280:
281: if (node instanceof Product) {
282: version = ((Product) node).getVersion().toString();
283: platforms = StringUtils.asString(((Product) node)
284: .getPlatforms(), " ");
285: title = StringUtils.asString(((Product) node)
286: .getPlatforms());
287: type = "component";
288:
289: id = uid + "_" + version + "_"
290: + platforms.replace(" ", "_") + "_" + type;
291: }
292:
293: if (node instanceof Group) {
294: type = "group";
295:
296: id = uid + "_" + type;
297: }
298:
299: out.println(" <tr id=\"" + id + "\">");
300:
301: out
302: .println(" <td class=\"tree-handle\"><img src=\""
303: + treeHandle
304: + "\" onclick=\"_expand('"
305: + id + "-children')\"/></td>");
306: out
307: .println(" <td class=\"icon\"><img src=\""
308: + icon + "\"/></td>");
309: if (version != null) {
310: out
311: .println(" <td class=\"checkbox\"><input type=\"checkbox\" name=\"component\" value=\""
312: + uid + "," + version + "\"/></td>");
313: } else {
314: out
315: .println(" <td class=\"checkbox\"></td>");
316: }
317: out
318: .println(" <td class=\"display-name\" title=\""
319: + title + "\">" + displayName + "</td>");
320:
321: out.println(" </tr>");
322:
323: if (node.getChildren().size() > 0) {
324: out.println(" <tr id=\"" + id
325: + "-children\">");
326:
327: out
328: .println(" <td class=\"tree-handle\"></td>");
329: out
330: .println(" <td colspan=\"3\" class=\"children\">");
331: out
332: .println(" <table class=\"registry\">");
333: buildRegistryNodes(out, node.getChildren(), platform);
334: out.println(" </table>");
335: out.println(" </td>");
336:
337: out.println(" </tr>");
338: }
339: }
340: }
341: }
|