|
Java example source code file (ServletUtils.java)
The ServletUtils.java Java example source code
/**
* Copyright (C) 2012 Google Inc.
*
* 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.google.inject.servlet;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.net.UrlEscapers;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
/**
* Some servlet utility methods.
*
* @author ntang@google.com (Michael Tang)
*/
final class ServletUtils {
private static final Splitter SLASH_SPLITTER = Splitter.on('/');
private static final Joiner SLASH_JOINER = Joiner.on('/');
private ServletUtils() {
// private to prevent instantiation.
}
/**
* Gets the context path relative path of the URI. Returns the path of the
* resource relative to the context path for a request's URI, or null if no
* path can be extracted.
*
* <p>Also performs url decoding and normalization of the path.
*/
// @Nullable
static String getContextRelativePath(
// @Nullable
final HttpServletRequest request) {
if (request != null) {
String contextPath = request.getContextPath();
String requestURI = request.getRequestURI();
if (contextPath.length() < requestURI.length()) {
String suffix = requestURI.substring(contextPath.length());
return normalizePath(suffix);
} else if (requestURI.trim().length() > 0 &&
contextPath.length() == requestURI.length()) {
return "/";
}
}
return null;
}
/**
* Normalizes a path by unescaping all safe, percent encoded characters.
*/
static String normalizePath(String path) {
StringBuilder sb = new StringBuilder(path.length());
int queryStart = path.indexOf('?');
String query = null;
if (queryStart != -1) {
query = path.substring(queryStart);
path = path.substring(0, queryStart);
}
// Normalize the path. we need to decode path segments, normalize and rejoin in order to
// 1. decode and normalize safe percent escaped characters. e.g. %70 -> 'p'
// 2. decode and interpret dangerous character sequences. e.g. /%2E/ -> '/./' -> '/'
// 3. preserve dangerous encoded characters. e.g. '/%2F/' -> '///' -> '/%2F'
List<String> segments = new ArrayList
Other Java examples (source code examples)Here is a short list of links related to this Java ServletUtils.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.