001: /*
002: * PublishHandler.java
003: *
004: * Brazil project web application Framework,
005: * export version: 1.1
006: * Copyright (c) 1999-2000 Sun Microsystems, Inc.
007: *
008: * Sun Public License Notice
009: *
010: * The contents of this file are subject to the Sun Public License Version
011: * 1.0 (the "License"). You may not use this file except in compliance with
012: * the License. A copy of the License is included as the file "license.terms",
013: * and also available at http://www.sun.com/
014: *
015: * The Original Code is from:
016: * Brazil project web application Framework release 1.1.
017: * The Initial Developer of the Original Code is: suhler.
018: * Portions created by suhler are Copyright (C) Sun Microsystems, Inc.
019: * All Rights Reserved.
020: *
021: * Contributor(s): cstevens, suhler.
022: *
023: * Version: 1.13
024: * Created by suhler on 99/03/31
025: * Last modified by suhler on 00/12/11 13:27:09
026: */
027:
028: package sunlabs.brazil.handler;
029:
030: import java.io.File;
031: import java.io.FileOutputStream;
032: import java.io.IOException;
033: import java.util.Properties;
034: import java.util.StringTokenizer;
035: import sunlabs.brazil.server.FileHandler;
036: import sunlabs.brazil.server.Handler;
037: import sunlabs.brazil.server.Request;
038: import sunlabs.brazil.server.Server;
039:
040: /**
041: * Handler for supporting publishing from Communicator.
042: * Launches an authentication handler to protect the content
043: * from malicious users.
044: * <p>
045: * Looks for <code>PUT</code> requests, and creates or modifies the
046: * content as indicated.
047: * <p>
048: * The following request properties are used:
049: * <dl class=props>
050: * <dt>prefix <dd> The URL prefix required for all documents
051: * <dt>session <dd> The the name of request property holding the session
052: * information to provide the credentials for posting. The
053: * default is "SessionID".
054: * </dl>
055: *
056: * @author Stephen Uhler
057: * @version 1.13, 00/12/11
058: */
059:
060: public class PublishHandler implements Handler {
061: private static final String PREFIX = "prefix"; // URL prefix for proxy
062: private static final String SESSION = "session";
063:
064: public String urlPrefix = "";
065: public String session = "SessionID";
066:
067: public String propsPrefix; // my prefix into the properties file
068:
069: /**
070: * Start up the authentication handler.
071: */
072:
073: public boolean init(Server server, String prefix) {
074: this .propsPrefix = prefix;
075:
076: Properties props = server.props;
077: urlPrefix = props.getProperty(prefix + PREFIX, urlPrefix);
078: session = props.getProperty(prefix + SESSION, session);
079:
080: return true;
081: }
082:
083: /**
084: * Make sure this is one of our "PUT" requests.
085: * Look up the credentials for this request.
086: * If no credentials are found, prompt the user for them.
087: * IF OK, save file to proper spot.
088: */
089:
090: public boolean respond(Request request) throws IOException {
091: if (!request.url.startsWith(urlPrefix)
092: || !request.method.equals("PUT")) {
093: return false;
094: }
095:
096: /*
097: * screen out bad requests
098: */
099:
100: if (request.postData == null) {
101: request.sendError(400, "No content to put");
102: return true;
103: }
104:
105: if (request.headers.get("Content-Range") != null) {
106: request.sendError(501, "Can't handle partial puts");
107: return true;
108: }
109:
110: /*
111: * Get the credentials left by the auth handler.
112: */
113:
114: String credentials = request.props.getProperty(session, "");
115:
116: /*
117: * Make sure the credentials are valid for this prefix
118: */
119:
120: StringTokenizer st = new StringTokenizer(credentials);
121: boolean ok = false;
122: while (st.hasMoreTokens()) {
123: String prefix = st.nextToken();
124: if (request.url.startsWith(prefix)) {
125: ok = true;
126: request.log(Server.LOG_DIAGNOSTIC, propsPrefix,
127: "file: " + request.url + " matches: " + prefix);
128: break;
129: }
130: }
131:
132: /*
133: * OK, now try to save the file.
134: */
135:
136: String root = request.props.getProperty(FileHandler.ROOT, ".");
137: File file = new File(root + FileHandler.urlToPath(request.url));
138:
139: request.log(Server.LOG_INFORMATIONAL, propsPrefix + "root: "
140: + root);
141:
142: int code = (file.exists()) ? 204 : 201;
143:
144: FileOutputStream out;
145: try {
146: out = new FileOutputStream(file);
147: } catch (IOException e) {
148: request.sendError(403, "Permission denied", file
149: .getAbsolutePath());
150: return true;
151: }
152: out.write(request.postData);
153: out.close();
154: request.sendResponse("Update of " + request.url + " succeeded",
155: "text/html", code);
156: return true;
157: }
158: }
|