org.apache.hadoop.util.ServletUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hadoop-apache Show documentation
Show all versions of hadoop-apache Show documentation
Shaded version of Apache Hadoop for Presto
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.hadoop.util;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Calendar;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import io.prestosql.hadoop.$internal.org.apache.commons.httpclient.URIException;
import io.prestosql.hadoop.$internal.org.apache.commons.httpclient.util.URIUtil;
import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability;
import io.prestosql.hadoop.$internal.com.google.common.base.Preconditions;
@InterfaceAudience.Private
@InterfaceStability.Unstable
public class ServletUtil {
/**
* Initial HTML header
*/
public static PrintWriter initHTML(ServletResponse response, String title
) throws IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("\n"
+ "\n"
+ "" + title + " \n"
+ "\n"
+ "" + title + "
\n");
return out;
}
/**
* Get a parameter from a ServletRequest.
* Return null if the parameter contains only white spaces.
*/
public static String getParameter(ServletRequest request, String name) {
String s = request.getParameter(name);
if (s == null) {
return null;
}
s = s.trim();
return s.length() == 0? null: s;
}
/**
* @return a long value as passed in the given parameter, throwing
* an exception if it is not present or if it is not a valid number.
*/
public static long parseLongParam(ServletRequest request, String param)
throws IOException {
String paramStr = request.getParameter(param);
if (paramStr == null) {
throw new IOException("Invalid request has no " + param + " parameter");
}
return Long.parseLong(paramStr);
}
public static final String HTML_TAIL = "
\n"
+ "Hadoop, "
+ Calendar.getInstance().get(Calendar.YEAR) + ".\n"
+ "";
/**
* HTML footer to be added in the jsps.
* @return the HTML footer.
*/
public static String htmlFooter() {
return HTML_TAIL;
}
/**
* Escape and encode a string regarded as within the query component of an URI.
* @param value the value to encode
* @return encoded query, null if the default charset is not supported
*/
public static String encodeQueryValue(final String value) {
try {
return URIUtil.encodeWithinQuery(value, "UTF-8");
} catch (URIException e) {
throw new AssertionError("JVM does not support UTF-8"); // should never happen!
}
}
/**
* Escape and encode a string regarded as the path component of an URI.
* @param path the path component to encode
* @return encoded path, null if UTF-8 is not supported
*/
public static String encodePath(final String path) {
try {
return URIUtil.encodePath(path, "UTF-8");
} catch (URIException e) {
throw new AssertionError("JVM does not support UTF-8"); // should never happen!
}
}
/**
* Decode a string regarded as the path component of an URI.
*
* @param path the path component to decode
* @return decoded path, null if UTF-8 is not supported
* @throws URISyntaxException
*/
public static String decodePath(final String path) {
try {
return new URI(path).getPath();
} catch (URISyntaxException e) {
throw new AssertionError("Failed to decode URI: " + path);
}
}
/**
* Parse and decode the path component from the given request.
* @param request Http request to parse
* @param servletName the name of servlet that precedes the path
* @return decoded path component, null if UTF-8 is not supported
*/
public static String getDecodedPath(final HttpServletRequest request, String servletName) {
try {
return URIUtil.decode(getRawPath(request, servletName), "UTF-8");
} catch (URIException e) {
throw new AssertionError("JVM does not support UTF-8"); // should never happen!
}
}
/**
* Parse the path component from the given request and return w/o decoding.
* @param request Http request to parse
* @param servletName the name of servlet that precedes the path
* @return path component, null if the default charset is not supported
*/
public static String getRawPath(final HttpServletRequest request, String servletName) {
Preconditions.checkArgument(request.getRequestURI().startsWith(servletName+"/"));
return request.getRequestURI().substring(servletName.length());
}
}