containerrequest.java

来自「resetful样式的ws样例,一种面向资源的webservices服务」· Java 代码 · 共 461 行 · 第 1/2 页

JAVA
461
字号
/* * * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. *  * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. *  * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development * and Distribution License("CDDL") (collectively, the "License").  You * may not use this file except in compliance with the License. You can obtain * a copy of the License at https://jersey.dev.java.net/CDDL+GPL.html * or jersey/legal/LICENSE.txt.  See the License for the specific * language governing permissions and limitations under the License. *  * When distributing the software, include this License Header Notice in each * file and include the License file at jersey/legal/LICENSE.txt. * Sun designates this particular file as subject to the "Classpath" exception * as provided by Sun in the GPL Version 2 section of the License file that * accompanied this code.  If applicable, add the following below the License * Header, with the fields enclosed by brackets [] replaced by your own * identifying information: "Portions Copyrighted [year] * [name of copyright owner]" *  * Contributor(s): *  * If you wish your version of this file to be governed by only the CDDL or * only the GPL Version 2, indicate your decision by adding "[Contributor] * elects to include this software in this distribution under the [CDDL or GPL * Version 2] license."  If you don't indicate a single choice of license, a * recipient has the option to distribute your version of this file under * either the CDDL, the GPL Version 2 or to extend the choice of license to * its licensees as provided above.  However, if you add GPL Version 2 code * and therefore, elected the GPL Version 2 license, then the option applies * only if the new code is made subject to such option by the copyright * holder. */package com.sun.jersey.spi.container;import com.sun.jersey.api.Responses;import com.sun.jersey.api.core.HttpRequestContext;import com.sun.jersey.impl.MultivaluedMapImpl;import com.sun.jersey.impl.VariantSelector;import com.sun.jersey.impl.http.header.HttpHeaderFactory;import com.sun.jersey.impl.http.header.reader.HttpHeaderReader;import com.sun.jersey.impl.model.HttpHelper;import java.io.IOException;import java.io.InputStream;import java.lang.annotation.Annotation;import java.lang.reflect.Type;import java.net.URI;import java.security.Principal;import java.text.ParseException;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.logging.Logger;import javax.ws.rs.WebApplicationException;import javax.ws.rs.core.Cookie;import javax.ws.rs.core.EntityTag;import javax.ws.rs.core.MediaType;import javax.ws.rs.core.MultivaluedMap;import javax.ws.rs.core.Response;import javax.ws.rs.core.Response.ResponseBuilder;import javax.ws.rs.core.UriBuilder;import javax.ws.rs.core.Variant;import javax.ws.rs.ext.MessageBodyReader;import javax.ws.rs.ext.MessageBodyWorkers;/** * Containers instantiate, or inherit, and provide an instance to the  * {@link WebApplication}. * * @author Paul.Sandoz@Sun.Com */public class ContainerRequest implements HttpRequestContext {    private static final Logger LOGGER = Logger.getLogger(ContainerRequest.class.getName());        private static final Annotation[] EMTPTY_ANNOTATIONS = new Annotation[0];        private final MessageBodyWorkers bodyContext;        private String method;        private InputStream entity;        private URI baseUri;        private URI requestUri;        private URI absolutePathUri;        private InBoundHeaders headers;    private int headersModCount;        private MediaType contentType;        private List<MediaType> accept;        private Map<String, Cookie> cookies;        private MultivaluedMap<String, String> cookieNames;        /**     * Create a new container request.     * <p>     * The base URI and the request URI must contain the same scheme, user info,      * host and port components.      *      * The base URI must not contain the query and fragment components. The      * encoded path component of the request URI must start with the encoded      * path component of the base URI. The encoded path component of the base      * URI must end in a '/' character.     *      * @param wa the web application     * @param method the HTTP method     * @param baseUri the base URI of the request     * @param requestUri the request URI     * @param headers the request headers     * @param entity the InputStream of the request entity     */    public ContainerRequest(            WebApplication wa,            String method,            URI baseUri,            URI requestUri,            InBoundHeaders headers,            InputStream entity) {                this.bodyContext = wa.getMessageBodyWorkers();        this.method = method;        this.baseUri = baseUri;        this.requestUri = requestUri;        this.headers = headers;        this.headersModCount = headers.getModCount();        this.entity = entity;            }        // ContainerRequest        /**     * Set the HTTP method.     * @param method the method.     */    public void setMethod(String method) {        this.method = method;    }    /**     * Set the base and request URI.     *      * @param baseUri the base URI.     * @param requestUri the (complete) request URI.     */    public void setUris(URI baseUri, URI requestUri) {        this.baseUri = baseUri;        this.requestUri = requestUri;                // reset state        absolutePathUri = null;    }        /**     * Set the input stream of the entity.     * @param entity the input stream of the entity.     */    public void setEntity(InputStream entity) {        this.entity = entity;    }                /**     * Set the request headers.     *      * @param headers the request headers.     */    public void setHeaders(InBoundHeaders headers) {        this.headers = headers;        this.headersModCount = headers.getModCount();                // reset state        contentType = null;        accept = null;        cookies = null;        cookieNames = null;            }            // HttpRequestContext        public URI getBaseUri() {        return baseUri;    }           public URI getRequestUri() {        return requestUri;    }        public URI getAbsolutePath() {        if (absolutePathUri != null) return absolutePathUri;                return absolutePathUri = UriBuilder.fromUri(requestUri).encode(false).                replaceQueryParams("").fragment("").                build();            }        public String getHeaderValue(String name) {        final List<String> v = getRequestHeaders().get(name);        if (v == null) return null;                if (v.isEmpty()) return "";                        if (v.size() == 1) return v.get(0);                StringBuilder sb = new StringBuilder(v.get(0));        for (int i = 1; i < v.size(); i++) {            final String s = v.get(i);            if (s.length() > 0)                sb.append(',').append(s);        }        return sb.toString();    }    public <T> T getEntity(Class<T> type, Type genericType, Annotation[] as) {        try {            MediaType mediaType = getMediaType();            MessageBodyReader<T> bw = bodyContext.getMessageBodyReader(                    type, genericType,                     as, mediaType);            if (bw == null) {                LOGGER.severe("A message body reader for Java type, " + type + 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?