/*
This file is part of Subsonic.
Subsonic is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Subsonic is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Subsonic. If not, see
application/octet-stream
is returned.
*/
public static String getMimeType(String suffix) {
for (String[] map : MIME_TYPES) {
if (map[0].equalsIgnoreCase(suffix) || ('.' + map[0]).equalsIgnoreCase(suffix)) {
return map[1];
}
}
return "application/octet-stream";
}
/**
* Converts a byte-count to a formatted string suitable for display to the user.
* For instance:
* format(918)
returns "918 B".format(98765)
returns "96 KB".format(1238476)
returns "1.2 MB"."u2 rem "greatest hits""
will return an array with
* three elements: {"u2", "rem", "greatest hits"}
*
* @param input The input string.
* @return Array of elements.
*/
public static String[] split(String input) {
if (input == null) {
return new String[0];
}
Pattern pattern = Pattern.compile("\".*?\"|\\S+");
Matcher matcher = pattern.matcher(input);
Listint
array.
*
* @param s String consisting of integers separated by whitespace.
* @return The corresponding array of ints.
* @throws NumberFormatException If string contains non-parseable text.
*/
public static int[] parseInts(String s) {
if (s == null) {
return new int[0];
}
String[] strings = StringUtils.split(s);
int[] ints = new int[strings.length];
for (int i = 0; i < strings.length; i++) {
ints[i] = Integer.parseInt(strings[i]);
}
return ints;
}
/**
* Change protocol from "https" to "http" for the given URL. The port number is also changed,
* but not if the given URL is already "http".
*
* @param url The original URL.
* @param port The port number to use, for instance 443.
* @return The transformed URL.
* @throws MalformedURLException If the original URL is invalid.
*/
public static String toHttpUrl(String url, int port) throws MalformedURLException {
URL u = new URL(url);
if ("https".equals(u.getProtocol())) {
return new URL("http", u.getHost(), port, u.getFile()).toString();
}
return url;
}
/**
* Determines whether a is equal to b, taking null into account.
*
* @return Whether a and b are equal, or both null.
*/
public static boolean isEqual(Object a, Object b) {
return a == null ? b == null : a.equals(b);
}
/**
* Parses a locale from the given string.
*
* @param s The locale string. Should be formatted as per the documentation in {@link Locale#toString()}.
* @return The locale.
*/
public static Locale parseLocale(String s) {
if (s == null) {
return null;
}
String[] elements = s.split("_");
if (elements.length == 0) {
return new Locale(s, "", "");
}
if (elements.length == 1) {
return new Locale(elements[0], "", "");
}
if (elements.length == 2) {
return new Locale(elements[0], elements[1], "");
}
return new Locale(elements[0], elements[1], elements[2]);
}
/**
* URL-encodes the input value using UTF-8.
*/
public static String urlEncode(String s) {
try {
return URLEncoder.encode(s, StringUtil.ENCODING_UTF8);
} catch (UnsupportedEncodingException x) {
throw new RuntimeException(x);
}
}
/**
* Encodes the given string by using the hexadecimal representation of its UTF-8 bytes.
*
* @param s The string to encode.
* @return The encoded string.
*/
public static String utf8HexEncode(String s) {
if (s == null) {
return null;
}
byte[] utf8;
try {
utf8 = s.getBytes(ENCODING_UTF8);
} catch (UnsupportedEncodingException x) {
throw new RuntimeException(x);
}
return String.valueOf(Hex.encodeHex(utf8));
}
/**
* Decodes the given string by using the hexadecimal representation of its UTF-8 bytes.
*
* @param s The string to decode.
* @return The decoded string.
* @throws Exception If an error occurs.
*/
public static String utf8HexDecode(String s) throws Exception {
if (s == null) {
return null;
}
return new String(Hex.decodeHex(s.toCharArray()), ENCODING_UTF8);
}
/**
* Calculates the MD5 digest and returns the value as a 32 character hex string.
*
* @param s Data to digest.
* @return MD5 digest as a hex string.
*/
public static String md5Hex(String s) {
if (s == null) {
return null;
}
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
return new String(Hex.encodeHex(md5.digest(s.getBytes(ENCODING_UTF8))));
} catch (Exception x) {
throw new RuntimeException(x.getMessage(), x);
}
}
/**
* Returns the file part of an URL. For instance:
*
*
* getUrlFile("http://archive.ncsa.uiuc.edu:80/SDG/Software/Mosaic/Demo/url-primer.html")
*
*
* will return "url-primer.html".
*
* @param url The URL in question.
* @return The file part, or null
if no file can be resolved.
*/
public static String getUrlFile(String url) {
try {
String path = new URL(url).getPath();
if (StringUtils.isBlank(path) || path.endsWith("/")) {
return null;
}
File file = new File(path);
String filename = file.getName();
if (StringUtils.isBlank(filename)) {
return null;
}
return filename;
} catch (MalformedURLException x) {
return null;
}
}
/**
* Rewrites the URL by changing the protocol, host and port.
*
* @param urlToRewrite The URL to rewrite.
* @param urlWithProtocolHostAndPort Use protocol, host and port from this URL.
* @return The rewritten URL, or an unchanged URL if either argument is not a proper URL.
*/
public static String rewriteUrl(String urlToRewrite, String urlWithProtocolHostAndPort) {
if (urlToRewrite == null) {
return null;
}
try {
URL urlA = new URL(urlToRewrite);
URL urlB = new URL(urlWithProtocolHostAndPort);
URL result = new URL(urlB.getProtocol(), urlB.getHost(), urlB.getPort(), urlA.getFile());
return result.toExternalForm();
} catch (MalformedURLException x) {
return urlToRewrite;
}
}
/**
* Makes a given filename safe by replacing special characters like slashes ("/" and "\")
* with dashes ("-").
*
* @param filename The filename in question.
* @return The filename with special characters replaced by underscores.
*/
public static String fileSystemSafe(String filename) {
for (String s : FILE_SYSTEM_UNSAFE) {
filename = filename.replace(s, "-");
}
return filename;
}
/**
* Parses the given string as a HTTP header byte range. See chapter 14.36.1 in RFC 2068
* for details.
*
* Only a subset of the allowed syntaxes are supported. Only ranges which specify first-byte-pos
* are supported. The last-byte-pos is optional.
*
* @param range The range from the HTTP header, for instance "bytes=0-499" or "bytes=500-"
* @return A range object (using inclusive values). If the last-byte-pos is not given, the end of
* the returned range is {@link Long#MAX_VALUE}. The method returns null
if the syntax
* of the given range is not supported.
*/
public static LongRange parseRange(String range) {
if (range == null) {
return null;
}
Pattern pattern = Pattern.compile("bytes=(\\d+)-(\\d*)");
Matcher matcher = pattern.matcher(range);
if (matcher.matches()) {
String firstString = matcher.group(1);
String lastString = StringUtils.trimToNull(matcher.group(2));
long first = Long.parseLong(firstString);
long last = lastString == null ? Long.MAX_VALUE : Long.parseLong(lastString);
if (first > last) {
return null;
}
return new LongRange(first, last);
}
return null;
}
public static String removeMarkup(String s) {
if (s == null) {
return null;
}
return s.replaceAll("<.*?>", "");
}
public static String getRESTProtocolVersion() {
// TODO: Read from xsd.
return "1.8.0";
}
}