Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* 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.cxf.jaxrs.utils;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.PathSegment;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.ext.RuntimeDelegate;
import javax.ws.rs.ext.RuntimeDelegate.HeaderDelegate;
import org.apache.cxf.common.i18n.BundleUtils;
import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.common.util.PropertyUtils;
import org.apache.cxf.common.util.StringUtils;
import org.apache.cxf.common.util.UrlUtils;
import org.apache.cxf.helpers.CastUtils;
import org.apache.cxf.jaxrs.impl.HttpHeadersImpl;
import org.apache.cxf.jaxrs.impl.HttpServletRequestFilter;
import org.apache.cxf.jaxrs.impl.HttpServletResponseFilter;
import org.apache.cxf.jaxrs.impl.MetadataMap;
import org.apache.cxf.jaxrs.impl.PathSegmentImpl;
import org.apache.cxf.jaxrs.impl.RuntimeDelegateImpl;
import org.apache.cxf.jaxrs.model.ParameterType;
import org.apache.cxf.message.Message;
import org.apache.cxf.service.model.EndpointInfo;
import org.apache.cxf.transport.Destination;
import org.apache.cxf.transport.http.AbstractHTTPDestination;
import org.apache.cxf.transport.http.Headers;
import org.apache.cxf.transport.servlet.BaseUrlHelper;
public final class HttpUtils {
private static final ResourceBundle BUNDLE = BundleUtils.getBundle(HttpUtils.class);
private static final Logger LOG = LogUtils.getL7dLogger(HttpUtils.class);
private static final String REQUEST_PATH_TO_MATCH = "path_to_match";
private static final String REQUEST_PATH_TO_MATCH_SLASH = "path_to_match_slash";
private static final String HTTP_SCHEME = "http";
private static final String LOCAL_HOST_IP_ADDRESS = "127.0.0.1";
private static final String REPLACE_LOOPBACK_PROPERTY = "replace.loopback.address.with.localhost";
private static final String LOCAL_HOST_IP_ADDRESS_SCHEME = "://" + LOCAL_HOST_IP_ADDRESS;
private static final String ANY_IP_ADDRESS = "0.0.0.0";
private static final String ANY_IP_ADDRESS_SCHEME = "://" + ANY_IP_ADDRESS;
private static final int DEFAULT_HTTP_PORT = 80;
private static final Pattern ENCODE_PATTERN = Pattern.compile("%[0-9a-fA-F][0-9a-fA-F]");
private static final String CHARSET_PARAMETER = "charset";
private static final String DOUBLE_QUOTE = "\"";
// there are more of such characters, ex, '*' but '*' is not affected by UrlEncode
private static final String PATH_RESERVED_CHARACTERS = "=@/:!$&\'(),;~";
private static final String QUERY_RESERVED_CHARACTERS = "?/,";
private static final Set KNOWN_HTTP_VERBS_WITH_NO_REQUEST_CONTENT =
new HashSet<>(Arrays.asList(new String[]{"GET", "HEAD", "OPTIONS", "TRACE"}));
private static final Set KNOWN_HTTP_VERBS_WITH_NO_RESPONSE_CONTENT =
new HashSet<>(Arrays.asList(new String[]{"HEAD", "OPTIONS"}));
private HttpUtils() {
}
public static String urlDecode(String value, String enc) {
return UrlUtils.urlDecode(value, enc);
}
public static String urlDecode(String value) {
return UrlUtils.urlDecode(value);
}
public static String pathDecode(String value) {
return UrlUtils.pathDecode(value);
}
private static String componentEncode(String reservedChars, String value) {
StringBuilder buffer = null;
int length = value.length();
int startingIndex = 0;
for (int i = 0; i < length; i++) {
char currentChar = value.charAt(i);
if (reservedChars.indexOf(currentChar) != -1) {
if (buffer == null) {
buffer = new StringBuilder(length + 8);
}
// If it is going to be an empty string nothing to encode.
if (i != startingIndex) {
buffer.append(urlEncode(value.substring(startingIndex, i)));
}
buffer.append(currentChar);
startingIndex = i + 1;
}
}
if (buffer == null) {
return urlEncode(value);
}
if (startingIndex < length) {
buffer.append(urlEncode(value.substring(startingIndex, length)));
}
return buffer.toString();
}
public static String queryEncode(String value) {
return componentEncode(QUERY_RESERVED_CHARACTERS, value);
}
public static String urlEncode(String value) {
return urlEncode(value, StandardCharsets.UTF_8.name());
}
public static String urlEncode(String value, String enc) {
return UrlUtils.urlEncode(value, enc);
}
public static String pathEncode(String value) {
String result = componentEncode(PATH_RESERVED_CHARACTERS, value);
// URLEncoder will encode '+' to %2B but will turn ' ' into '+'
// We need to retain '+' and encode ' ' as %20
if (result.indexOf('+') != -1) {
result = result.replace("+", "%20");
}
if (result.indexOf("%2B") != -1) {
result = result.replace("%2B", "+");
}
return result;
}
public static boolean isPartiallyEncoded(String value) {
return ENCODE_PATTERN.matcher(value).find();
}
/**
* Encodes partially encoded string. Encode all values but those matching pattern
* "percent char followed by two hexadecimal digits".
*
* @param encoded fully or partially encoded string.
* @return fully encoded string
*/
public static String encodePartiallyEncoded(String encoded, boolean query) {
if (encoded.length() == 0) {
return encoded;
}
Matcher m = ENCODE_PATTERN.matcher(encoded);
if (!m.find()) {
return query ? HttpUtils.queryEncode(encoded) : HttpUtils.pathEncode(encoded);
}
int length = encoded.length();
StringBuilder sb = new StringBuilder(length + 8);
int i = 0;
do {
String before = encoded.substring(i, m.start());
sb.append(query ? HttpUtils.queryEncode(before) : HttpUtils.pathEncode(before));
sb.append(m.group());
i = m.end();
} while (m.find());
String tail = encoded.substring(i, length);
sb.append(query ? HttpUtils.queryEncode(tail) : HttpUtils.pathEncode(tail));
return sb.toString();
}
public static SimpleDateFormat getHttpDateFormat() {
return Headers.getHttpDateFormat();
}
public static String toHttpDate(Date date) {
return Headers.toHttpDate(date);
}
public static RuntimeDelegate getOtherRuntimeDelegate() {
try {
RuntimeDelegate rd = RuntimeDelegate.getInstance();
return rd instanceof RuntimeDelegateImpl ? null : rd;
} catch (Throwable t) {
return null;
}
}
public static HeaderDelegate