|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.examples.modules.explservlet; import java.awt.Graphics; import java.beans.*; import java.io.*; import java.net.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import org.openide.nodes.*; import java.awt.Image; import java.awt.image.BufferedImage; import javax.imageio.ImageIO; import org.openide.ErrorManager; import org.openide.filesystems.FileSystem; import org.openide.filesystems.Repository; import org.openide.loaders.DataFilter; import org.openide.loaders.DataObject; import org.openide.loaders.DataObjectNotFoundException; import org.openide.loaders.RepositoryNodeFactory; import org.openide.xml.XMLUtil; /* import java.awt.image.BufferedImage; import java.awt.Graphics; import com.sun.image.codec.jpeg.JPEGCodec; */ /** Serve the NetBeans Explorer over HTTP. * @author Jesse Glick */ public class ExplorerServlet extends HttpServlet { private Node root; public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { Node n = findNode(getRoot(), req.getPathInfo()); if (n == null) { res.sendError(404, "Could not find any node named: " + req.getPathInfo()); return; } if (Boolean.valueOf(req.getParameter("icon")).booleanValue()) { // Node icon. Image icon = n.getIcon(BeanInfo.ICON_COLOR_16x16); BufferedImage bi = new BufferedImage(icon.getWidth(null), icon.getHeight(null), BufferedImage.TYPE_INT_ARGB); Graphics g = bi.getGraphics(); g.drawImage(icon, 0, 0, null); g.dispose(); res.setContentType("image/png"); ImageIO.write(bi, "png", res.getOutputStream()); res.setContentType("image/jpeg"); } else if (req.getParameter("prop") != null) { // Property editor. res.setContentType("text/html"); PrintWriter pw = res.getWriter(); String propname = req.getParameter("prop"); Node.PropertySet[] propsets = n.getPropertySets(); FOUND: for (int i = 0; i < propsets.length; i++) { Node.Property[] props = propsets[i].getProperties(); for (int j = 0; j < props.length; j++) { Node.Property prop = props[j]; if (prop.getName().equals(propname)) { pw.println("Back to node: " + XMLUtil.toElementContent(n.getDisplayName()) + ""); pw.println(""); break FOUND; } } } } else { // Info about the node and its children. res.setContentType("text/html"); PrintWriter pw = res.getWriter(); try { pw.println("Node: " + XMLUtil.toElementContent(n.getDisplayName())); if (n.getParentNode() != null) { // XXX show full hierarchy down to here... pw.println(" Parent: " + XMLUtil.toElementContent(n.getParentNode().getDisplayName()) + ""); } if (!n.isLeaf()) { Children ch = n.getChildren(); pw.println(" Children:
" + XMLUtil.toElementContent(propsets[i].getDisplayName()) + ":"); Node.Property[] props = propsets[i].getProperties(); pw.println(""); for (int j = 0; j < props.length; j++) { if (!props[j].canRead()) continue; pw.print(""); } pw.println("
"); re.printStackTrace(pw); pw.println(""); } pw.close(); } } public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { Node n = findNode(getRoot(), req.getPathInfo()); if (n == null) { res.sendError(404, "Could not find any node named: " + req.getPathInfo()); return; } Node.PropertySet[] propsets = n.getPropertySets(); for (int i = 0; i < propsets.length; i++) { Node.Property[] props = propsets[i].getProperties(); for (int j = 0; j < props.length; j++) { Node.Property prop = props[j]; String nue = req.getParameter(prop.getName()); if (nue != null) { PropertyEditor ed = prop.getPropertyEditor(); try { ed.setAsText(nue); prop.setValue(ed.getValue()); } catch (Exception e) { res.setContentType("text/html"); PrintWriter pw = res.getWriter(); pw.print("Problem setting value " + XMLUtil.toElementContent(nue) + " on property " + prop.getName() + ": "); e.printStackTrace(pw); pw.println(""); pw.println(" Back to node"); return; } // For some reason this does not work, nor "", nor "./", nor even "../nodename/"! //res.sendRedirect(req.getRequestURI()); // Fine, just handle it as a get request then: doGet(req, res); } } } res.setContentType("text/html"); PrintWriter pw = res.getWriter(); pw.println("No such property found"); } private static String encode(String in) { if (in.length() == 0) { return "%"; } else { return URLEncoder.encode(in); } } private static String decode(String in) { if (in.equals("%")) { return ""; } else { return URLDecoder.decode(in); } } private Node getRoot() { if (root == null) { Children.Array kids = new Children.Array(); FileSystem sfs = Repository.getDefault().getDefaultFileSystem(); try { kids.add(new Node[] { RepositoryNodeFactory.getDefault().repository(DataFilter.ALL).cloneNode(), DataObject.find(sfs.findResource("UI/Runtime")).getNodeDelegate().cloneNode(), DataObject.find(sfs.findResource("UI/Services")).getNodeDelegate().cloneNode(), // XXX project desktop / workplace? }); } catch (DataObjectNotFoundException e) { ErrorManager.getDefault().notify(e); } root = new AbstractNode(kids); root.setName("Root Node"); ((AbstractNode)root).setIconBase("org/netbeans/examples/modules/explservlet/explorer"); } return root; } private Node findNode(Node root, String path) { if (path == null) return null; if (path.equals("/")) return root; if (!path.startsWith("/")) return null; int idx = path.indexOf('/', 1); if (idx == -1) return null; Node first = root.getChildren().findChild(decode(path.substring(1, idx))); if (first == null) return null; return findNode(first, path.substring(idx)); } } |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.