/** * Checks if the produced content type matches. * * @param http http context * @return result of check */ private boolean produces(final HTTPContext http) { // return true if no type is given if (produces.isEmpty()) return true; // check if any combination matches for (final String pr : http.produces()) { for (final String p : produces) { if (MimeTypes.matches(p, pr)) return true; } } return false; }
/** * Checks if the consumed content type matches. * * @param http http context * @return result of check */ private boolean consumes(final HTTPContext http) { // return true if no type is given if (consumes.isEmpty()) return true; // return true if no content type is specified by the user final String ct = http.contentType(); if (ct == null) return true; // check if any combination matches for (final String c : consumes) { if (MimeTypes.matches(c, ct)) return true; } return false; }
/** * Lists resources of the specified database. * * @return success flag * @throws IOException I/O exception */ private boolean listDB() throws IOException { final String db = args[0]; final String path = args[1] != null ? args[1] : ""; if (!Databases.validName(db)) return error(NAME_INVALID_X, db); final Table table = new Table(); table.description = RESOURCES; table.header.add(INPUT_PATH); table.header.add(TYPE); table.header.add(MimeTypes.CONTENT_TYPE); table.header.add(SIZE); try { // add xml documents final Data data = Open.open(db, context); final Resources res = data.resources; final IntList il = res.docs(path); final int ds = il.size(); for (int i = 0; i < ds; i++) { final int pre = il.get(i); final TokenList tl = new TokenList(3); final byte[] file = data.text(pre, true); tl.add(file); tl.add(DataText.M_XML); tl.add(MimeTypes.APP_XML); tl.add(data.size(pre, Data.DOC)); table.contents.add(tl); } // add binary resources for (final byte[] file : res.binaries(path)) { final String f = string(file); final TokenList tl = new TokenList(3); tl.add(file); tl.add(DataText.M_RAW); tl.add(MimeTypes.get(f)); tl.add(data.meta.binary(f).length()); table.contents.add(tl); } Close.close(data, context); } catch (final IOException ex) { return error(Util.message(ex)); } out.println(table.sort().finish()); return true; }
public void run() { StringBuffer dirData = new StringBuffer(); StringBuffer logData = new StringBuffer(); try { File getFile = parseFileName(fileName); logData .append("Do GET: input=") .append(fileName) .append(", " + "parsed=") .append(getFile) .append(", "); String header; if (getFile.isDirectory()) { logData.append("directory located"); String files[] = getFile.list(); for (String file : files) { File f = new File(getFile, file); dirData.append(f.toString().substring(getFile.getParent().length())); dirData.append("\t"); if (f.isDirectory()) dirData.append("d"); else dirData.append("f"); dirData.append("\t"); dirData.append(f.length()); dirData.append("\t"); dirData.append(f.lastModified()); dirData.append("\n"); } fileLength = dirData.length(); String fileType = MimeTypes.getProperty("txt"); if (fileType == null) fileType = "application/java"; header = "HTTP/1.0 200 OK\n" + "Allow: GET\nMIME-Version: 1.0\n" + "Server: " + SERVER_DESCRIPTION + "\n" + "Content-Type: " + fileType + "\n" + "Content-Length: " + fileLength + "\r\n\r\n"; } else if (getFile.exists()) { requestedFile = new DataInputStream(new BufferedInputStream(new FileInputStream(getFile))); fileLength = requestedFile.available(); String fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()); fileType = MimeTypes.getProperty(fileType); header = "HTTP/1.0 200 OK\n" + "Allow: GET\nMIME-Version: 1.0\n" + "Server: " + SERVER_DESCRIPTION + "\n" + "Content-Type: " + fileType + "\n" + "Content-Length: " + fileLength + "\r\n\r\n"; } else { header = "HTTP/1.0 404 Not Found\r\n\r\n"; } DataOutputStream clientStream = new DataOutputStream(new BufferedOutputStream(client.getOutputStream())); clientStream.writeBytes(header); if (getFile.isDirectory()) { clientStream.writeBytes(dirData.toString()); } else if (getFile.exists()) { byte[] buffer = new byte[fileLength]; requestedFile.readFully(buffer); logData.append("file size: [").append(fileLength).append("]"); try { clientStream.write(buffer); } catch (Exception e) { String s = "Sending [" + getFile.getAbsolutePath() + "], " + "size [" + fileLength + "], " + "to client at " + "[" + client.getInetAddress().getHostAddress() + "]"; if (logger.isLoggable(Level.FINE)) logger.log(Level.FINE, s, e); if (debug) { System.out.println(s); e.printStackTrace(); } } requestedFile.close(); } else { logData.append("not found"); } if (debug) System.out.println(logData.toString()); if (logger.isLoggable(Level.FINE)) logger.fine(logData.toString()); clientStream.flush(); clientStream.close(); } catch (Exception e) { logger.log(Level.WARNING, "Closing Socket", e); } finally { try { client.close(); } catch (IOException e2) { logger.log(Level.WARNING, "Closing incoming socket", e2); } } } // end of GetFile
private void addMimeTypeStart() { String extension = mXpp.getAttributeValue(null, ATTR_EXTENSION); String mimetype = mXpp.getAttributeValue(null, ATTR_MIMETYPE); // Log.d("FileFilter:","addMimeTypeStart: "+extension+" "+mimetype ); mMimeTypes.put(extension, mimetype); }
public String getMimeType(String file) { int index = file.lastIndexOf('.'); return (index++ > 0) ? MimeTypes.getContentType(file.substring(index)) : "unkown/unkown"; }