/** * Maps a servlet-like URI to a jxp file. * * @param f File to check * @return new File with <root>.jxp if exists, orig file if not * @example /wiki/geir -> maps to wiki.jxp if exists */ File tryServlet(File f) { if (f.exists()) return f; String uri = f.toString(); if (uri.startsWith(_rootFile.toString())) uri = uri.substring(_rootFile.toString().length()); if (_core != null && uri.startsWith(_core._base.toString())) uri = "/~~" + uri.substring(_core._base.toString().length()); while (uri.startsWith("/")) uri = uri.substring(1); int start = 0; while (true) { int idx = uri.indexOf("/", start); if (idx < 0) break; String foo = uri.substring(0, idx); File temp = getFileSafe(foo + ".jxp"); if (temp != null && temp.exists()) f = temp; start = idx + 1; } return f; }
/** * Tries to find the given file, assuming that it's missing the ".jxp" extension * * @param f File to check * @return same file if not found to be missing the .jxp, or a new File w/ the .jxp appended */ File tryNoJXP(File f) { if (f.exists()) return f; if (f.getName().indexOf(".") >= 0) return f; File temp = new File(f.toString() + ".jxp"); return temp.exists() ? temp : f; }
File tryOtherExtensions(File f) { if (f.exists()) return f; if (f.getName().indexOf(".") >= 0) return f; for (int i = 0; i < JSFileLibrary._srcExtensions.length; i++) { File temp = new File(f.toString() + JSFileLibrary._srcExtensions[i]); if (temp.exists()) return temp; } return f; }
/** * Constructs a new MultipartRequest to handle the specified request, saving any uploaded files to * the given directory, and limiting the upload size to the specified length. If the content is * too large, an IOException is thrown. This constructor actually parses the * <tt>multipart/form-data</tt> and throws an IOException if there's any problem reading or * parsing the request. * * <p>To avoid file collisions, this constructor takes an implementation of the FileRenamePolicy * interface to allow a pluggable rename policy. * * @param request the servlet request. * @param saveDirectory the directory in which to save any uploaded files. * @param maxPostSize the maximum size of the POST content. * @param encoding the encoding of the response, such as ISO-8859-1 * @param policy a pluggable file rename policy * @exception IOException if the uploaded content is larger than <tt>maxPostSize</tt> or there's a * problem reading or parsing the request. */ public MultipartRequest( HttpServletRequest request, String saveDirectory, int maxPostSize, String encoding, FileRenamePolicy policy) throws IOException { // Sanity check values if (request == null) throw new IllegalArgumentException("request cannot be null"); if (saveDirectory == null) throw new IllegalArgumentException("saveDirectory cannot be null"); if (maxPostSize <= 0) { throw new IllegalArgumentException("maxPostSize must be positive"); } // Save the dir File dir = new File(saveDirectory); // Check saveDirectory is truly a directory if (!dir.isDirectory()) throw new IllegalArgumentException("Not a directory: " + saveDirectory); // Check saveDirectory is writable if (!dir.canWrite()) throw new IllegalArgumentException("Not writable: " + saveDirectory); // Parse the incoming multipart, storing files in the dir provided, // and populate the meta objects which describe what we found MultipartParser parser = new MultipartParser(request, maxPostSize, true, true, encoding); // Some people like to fetch query string parameters from // MultipartRequest, so here we make that possible. Thanks to // Ben Johnson, [email protected], for the idea. if (request.getQueryString() != null) { // Let HttpUtils create a name->String[] structure Hashtable queryParameters = HttpUtils.parseQueryString(request.getQueryString()); // For our own use, name it a name->Vector structure Enumeration queryParameterNames = queryParameters.keys(); while (queryParameterNames.hasMoreElements()) { Object paramName = queryParameterNames.nextElement(); String[] values = (String[]) queryParameters.get(paramName); Vector newValues = new Vector(); for (int i = 0; i < values.length; i++) { newValues.add(values[i]); } parameters.put(paramName, newValues); } } Part part; while ((part = parser.readNextPart()) != null) { String name = part.getName(); if (name == null) { throw new IOException("Malformed input: parameter name missing (known Opera 7 bug)"); } if (part.isParam()) { // It's a parameter part, add it to the vector of values ParamPart paramPart = (ParamPart) part; String value = paramPart.getStringValue(); Vector existingValues = (Vector) parameters.get(name); if (existingValues == null) { existingValues = new Vector(); parameters.put(name, existingValues); } existingValues.addElement(value); } else if (part.isFile()) { // It's a file part FilePart filePart = (FilePart) part; String fileName = filePart.getFileName(); if (fileName != null) { filePart.setRenamePolicy(policy); // null policy is OK // The part actually contained a file filePart.writeTo(dir); files.put( name, new UploadedFile( dir.toString(), filePart.getFileName(), fileName, filePart.getContentType())); } else { // The field did not contain a file files.put(name, new UploadedFile(null, null, null, null)); } } } }
/** * Initializes a new context for a given site directory. * * @param f the file to run */ public AppContext(File f) { this(f.toString()); }