📄 davutilities.java
字号:
/* ========================================================================== *
* Copyright (C) 2004-2005 Pier Fumagalli <http://www.betaversion.org/~pier/> *
* All rights reserved. *
* ========================================================================== *
* *
* Licensed under the Apache License, Version 2.0 (the "License"). You may *
* not use this file except in compliance with the License. You may obtain a *
* copy of the License at <http://www.apache.org/licenses/LICENSE-2.0>. *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT *
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the *
* License for the specific language governing permissions and limitations *
* under the License. *
* *
* ========================================================================== */
package com.sslexplorer.vfs.webdav;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.TimeZone;
import com.sslexplorer.boot.Util;
import com.sslexplorer.core.CoreUtil;
import com.sslexplorer.security.SessionInfo;
import com.sslexplorer.vfs.utils.URI;
import com.sslexplorer.vfs.utils.URI.MalformedURIException;
/**
* <p>
* A collection of static utilities.
* </p>
*
* @author <a href="http://www.betaversion.org/~pier/">Pier Fumagalli</a>
*/
public class DAVUtilities {
/**
* <p>
* A {@link String} of all acceptable characters in a URI.
* </p>
*/
// private static final String ACCEPTABLE = "ABCDEFGHIJLKMNOPQRSTUVWXYZ" +
// // ALPHA
// // (UPPER)
// "abcdefghijklmnopqrstuvwxyz" + // ALPHA (LOWER)
// "0123456789" + // DIGIT
// "_-!.~'()*" + // UNRESERVED
// ",;:$&+=" + // PUNCT
// "?/[]@"; // RESERVED
private static final String ACCEPTABLE = "ABCDEFGHIJLKMNOPQRSTUVWXYZ" + // ALPHA
// (UPPER)
"abcdefghijklmnopqrstuvwxyz" + // ALPHA (LOWER)
"0123456789" + // DIGIT
"_-!.~'()*" + // UNRESERVED
",;:$+=" + // PUNCT
"?/@"; // RESERVED
/**
* <p>
* A {@link HashMap} of configured mime types.
* </p>
*/
private static Map MIME_TYPES = new HashMap();
/**
* <p>
* A {@link HashMap} of configured mime types.
* </p>
*/
private static Properties PROPERTIES = new Properties();
/**
* <p>
* The {@link SimpleDateFormat} RFC-822 date format.
* </p>
*/
private static final String FORMAT_822 = "EEE, dd MMM yyyy HH:mm:ss 'GMT'";
/**
* <p>
* The {@link TimeZone} to use for formatting RFC-822 dates.
* </p>
*/
private static final TimeZone TIMEZONE_822 = TimeZone.getTimeZone("GMT");
/**
* <p>
* Load the mime types map from a resource.
* </p>
*/
static {
InputStream prop = DAVUtilities.class.getResourceAsStream("webdav.props");
try {
DAVUtilities.PROPERTIES.load(prop);
prop.close();
} catch (Exception exception) {
exception.printStackTrace();
}
/* Load up the mime types table */
InputStream mime = DAVUtilities.class.getResourceAsStream("mime.types");
try {
InputStreamReader read = new InputStreamReader(mime);
BufferedReader buff = new BufferedReader(read);
String line = null;
while ((line = buff.readLine()) != null) {
line = line.trim();
if (line.length() == 0)
continue;
if (line.charAt(0) == '#')
continue;
StringTokenizer tokenizer = new StringTokenizer(line);
if (tokenizer.countTokens() > 1) {
String type = tokenizer.nextToken();
while (tokenizer.hasMoreTokens()) {
String extension = '.' + tokenizer.nextToken();
DAVUtilities.MIME_TYPES.put(extension, type);
}
}
}
buff.close();
read.close();
mime.close();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* <p>
* Deny public construction of {@link DAVUtilities} instances.
* </p>
*/
private DAVUtilities() {
super();
}
/**
* <p>
* Return the value of a property configured for this package.
* </p>
*
* @param name the property name
* @return a {@link String} instance or <b>null</b> if unknown.
*/
public static String getProperty(String name) {
if (name == null)
return null;
return DAVUtilities.PROPERTIES.getProperty(name);
}
/**
* <p>
* Return the MIME Type configured for a given resource.
* </p>
*
* @param name the resource name whose MIME Type needs to be looked up.
* @return a {@link String} instance or <b>null</b> if the type is unknown.
*/
public static String getMimeType(String name) {
if (name == null)
return null;
Iterator iterator = DAVUtilities.MIME_TYPES.keySet().iterator();
while (iterator.hasNext()) {
String extension = (String) iterator.next();
if (name.endsWith(extension)) {
return (String) DAVUtilities.MIME_TYPES.get(extension);
}
}
return null;
}
/**
* <p>
* Return a {@link String} message given an HTTP status code.
* </p>
*
* @param status status code
* @return status strings
*/
public static String getStatusMessage(int status) {
switch (status) {
/* HTTP/1.1 RFC-2616 */
case 100:
return "100 Continue";
case 101:
return "101 Switching Protocols";
case 200:
return "200 OK";
case 201:
return "201 Created";
case 202:
return "202 Accepted";
case 203:
return "203 Non-Authoritative Information";
case 204:
return "204 No Content";
case 205:
return "205 Reset Content";
case 206:
return "206 Partial Content";
case 300:
return "300 Multiple Choices";
case 301:
return "301 Moved Permanently";
case 302:
return "302 Found";
case 303:
return "303 See Other";
case 304:
return "304 Not Modified";
case 305:
return "305 Use Proxy";
case 306:
return "306 (Unused)";
case 307:
return "307 Temporary Redirect";
case 400:
return "400 Bad Request";
case 401:
return "401 Unauthorized";
case 402:
return "402 Payment Required";
case 403:
return "403 Forbidden";
case 404:
return "404 Not Found";
case 405:
return "405 Method Not Allowed";
case 406:
return "406 Not Acceptable";
case 407:
return "407 Proxy Authentication Required";
case 408:
return "408 Request Timeout";
case 409:
return "409 Conflict";
case 410:
return "410 Gone";
case 411:
return "411 Length Required";
case 412:
return "412 Precondition Failed";
case 413:
return "413 Request Entity Too Large";
case 414:
return "414 Request-URI Too Long";
case 415:
return "415 Unsupported Media Type";
case 416:
return "416 Requested Range Not Satisfiable";
case 417:
return "417 Expectation Failed";
case 500:
return "500 Internal Server Error";
case 501:
return "501 Not Implemented";
case 502:
return "502 Bad Gateway";
case 503:
return "503 Service Unavailable";
case 504:
return "504 Gateway Timeout";
case 505:
return "505 HTTP Version Not Supported";
/* DAV/1.0 RFC-2518 */
case 102:
return "102 Processing";
case 207:
return "207 Multi-Status";
case 422:
return "422 Unprocessable Entity";
case 423:
return "423 Locked";
case 424:
return "424 Failed Dependency";
case 507:
return "507 Insufficient Storage";
/* Unknown */
default:
return null;
}
}
/**
* <p>
* Format an {@link Object} according to the HTTP/1.1 RFC.
* </p>
*
* @param object the {@link Object} to format.
* @return a {@link String} instance or <b>null</b> if the object was null.
*/
public static String format(Object object) {
if (object == null)
return null;
if (object instanceof String)
return ((String) object);
if (object instanceof Date) {
SimpleDateFormat formatter = new SimpleDateFormat(FORMAT_822);
formatter.setTimeZone(TIMEZONE_822);
return formatter.format((Date) object);
}
return (object.toString());
}
/**
* <p>
* Return the HEX representation of an array of bytes.
* </p>
*
* @param buffer the array of bytes to convert in a HEX {@link String}.
* @return a <b>non-null</b> {@link String} instance.
*/
public static String toHexString(byte buffer[]) {
char output[] = new char[buffer.length * 2];
int position = 0;
for (int x = 0; x < buffer.length; x++) {
output[position++] = DAVUtilities.toHexDigit(buffer[x] >> 4);
output[position++] = DAVUtilities.toHexDigit(buffer[x]);
}
return new String(output);
}
/**
* <p>
* Return the HEX representation of a long integer.
* </p>
*
* @param number the long to convert in a HEX {@link String}.
* @return a <b>non-null</b> 16-characters {@link String} instance.
*/
public static String toHexString(long number) {
char output[] = new char[16];
output[0] = DAVUtilities.toHexDigit((int) (number >> 60));
output[1] = DAVUtilities.toHexDigit((int) (number >> 56));
output[2] = DAVUtilities.toHexDigit((int) (number >> 52));
output[3] = DAVUtilities.toHexDigit((int) (number >> 48));
output[4] = DAVUtilities.toHexDigit((int) (number >> 44));
output[5] = DAVUtilities.toHexDigit((int) (number >> 40));
output[6] = DAVUtilities.toHexDigit((int) (number >> 36));
output[7] = DAVUtilities.toHexDigit((int) (number >> 32));
output[8] = DAVUtilities.toHexDigit((int) (number >> 28));
output[9] = DAVUtilities.toHexDigit((int) (number >> 24));
output[10] = DAVUtilities.toHexDigit((int) (number >> 20));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -