PropertiesManager.java
PropertiesManager.java |
1 /* 2 * Copyright (c) 2009, Sun Microsystems 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * Neither the name of the Sun Microsystems nor the names of its contributors 16 * may be used to endorse or promote products derived from this software without 17 * specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 package org.sunspotworld.demo; 33 34 import com.sun.spot.peripheral.Spot; 35 import com.sun.spot.util.Properties; 36 import com.sun.squawk.VM; 37 import com.sun.squawk.util.StringTokenizer; 38 import java.io.ByteArrayInputStream; 39 import java.io.IOException; 40 import java.util.Enumeration; 41 import com.sun.spot.wot.WebApplication; 42 import com.sun.spot.wot.http.HttpResponse; 43 import com.sun.spot.wot.http.HttpRequest; 44 45 /** 46 * 47 * @author vgupta 48 */ 49 public class PropertiesManager extends WebApplication { 50 public void init() {}; 51 52 public PropertiesManager(String str) { 53 super(str); 54 } 55 56 public synchronized HttpResponse processRequest(HttpRequest request) throws IOException { 57 HttpResponse response = new HttpResponse(); 58 String path = request.getPathInfo(); 59 StringBuffer sb = null; 60 61 if (request.getMethod().equalsIgnoreCase("GET")) { 62 if (path.equals("/") || path.equals("")) { 63 response.setBody(getPropertiesResponse(null).toString().getBytes()); 64 } else { 65 sb = getPropertiesResponse(path.substring(1)); 66 if (sb == null) { 67 response.setStatus(HttpResponse.SC_NOT_FOUND); 68 response.setHeader("Cache-Control", "max-age=10"); 69 return response; 70 } else { 71 response.setBody(sb.toString().getBytes()); 72 } 73 } 74 75 response.setStatus(HttpResponse.SC_OK); 76 response.setHeader("Content-Type", "text/plain"); 77 response.setHeader("Cache-Control", "max-age=10"); 78 return response; 79 } else if (request.getMethod().equalsIgnoreCase("POST")) { 80 if (!path.equals("/") && !path.equals("")) { 81 response.setStatus(HttpResponse.SC_BAD_REQUEST); 82 return response; 83 } else { 84 Properties oldProps = Spot.getInstance().getPersistentProperties(); 85 Properties newProps = new Properties(); 86 newProps.load(new ByteArrayInputStream(request.getBody())); 87 Enumeration newKeys = newProps.keys(); 88 while (newKeys.hasMoreElements()) { 89 String newKey = (String) newKeys.nextElement(); 90 oldProps.setProperty(newKey, 91 newProps.getProperty(newKey)); 92 } 93 94 Spot.getInstance().storeProperties(oldProps); 95 96 response.setBody(getPropertiesResponse(null).toString().getBytes()); 97 response.setStatus(HttpResponse.SC_OK); 98 response.setHeader("Cache-Control", "max-age=10"); 99 return response; 100 } 101 } else if (request.getMethod().equalsIgnoreCase("DELETE")) { 102 if (path.equals("/") || path.equals("")) { 103 response.setStatus(HttpResponse.SC_BAD_REQUEST); 104 return response; 105 } else { 106 Properties oldProps = Spot.getInstance().getPersistentProperties(); 107 if (oldProps.containsKey(path.substring(1))) { 108 oldProps.remove(path.substring(1)); 109 Spot.getInstance().storeProperties(oldProps); 110 response.setBody(getPropertiesResponse(null).toString().getBytes()); 111 response.setStatus(HttpResponse.SC_OK); 112 response.setHeader("Content-Type", "text/plain"); 113 response.setHeader("Cache-Control", "max-age=10"); 114 return response; 115 } else { 116 response.setStatus(HttpResponse.SC_NOT_FOUND); 117 response.setHeader("Cache-Control", "max-age=10"); 118 return response; 119 } 120 } 121 } else { 122 response.setStatus(HttpResponse.SC_METHOD_NOT_ALLOWED); 123 response.setHeader("Allow", "GET, DELETE, POST"); 124 125 return response; 126 } 127 } 128 129 private StringBuffer getPropertiesResponse(String key) { 130 Properties prop = Spot.getInstance().getPersistentProperties(); 131 StringBuffer sb = new StringBuffer(); 132 String value; 133 134 if (key != null) { 135 if ((value = prop.getProperty(key)) == null) 136 return null; 137 else 138 sb.append("\"" + value + "\""); 139 140 return sb; 141 } 142 143 sb.append("{\n"); 144 if (prop != null) { 145 for (Enumeration e = prop.propertyNames(); e.hasMoreElements();) { 146 key = (String) e.nextElement(); 147 if (key == null) continue; 148 value = prop.getProperty(key); 149 sb.append("\t\"" + key + "\": \"" + value + "\",\n"); 150 } 151 } 152 153 sb.append("}\n"); 154 return sb; 155 } 156 } 157