containerrequest.java

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

JAVA
461
字号
                        ", and MIME media type, " + mediaType + ", was not found");                                    throw new WebApplicationException(                        Responses.unsupportedMediaType().build());            }            return bw.readFrom(type, genericType, as, mediaType, headers, entity);        } catch (IOException e) {            throw new IllegalArgumentException(e);        }    }        public <T> T getEntity(Class<T> type) {        return getEntity(type, type, EMTPTY_ANNOTATIONS);    }        public String getHttpMethod() {        return method;    }        public MediaType getAcceptableMediaType(List<MediaType> mediaTypes) {        if (mediaTypes.isEmpty())            return getAcceptableMediaTypes().get(0);                for (MediaType a : getAcceptableMediaTypes()) {            if (a.getType().equals(MediaType.MEDIA_TYPE_WILDCARD))                return mediaTypes.get(0);                        for (MediaType m : mediaTypes)                if (m.isCompatible(a)) return m;        }                return null;    }    public MultivaluedMap<String, String> getCookieNameValueMap() {        if (cookieNames == null || headersModCount != headers.getModCount()) {            cookieNames = new MultivaluedMapImpl();            for (Map.Entry<String, Cookie> e : getCookies().entrySet()) {                cookieNames.putSingle(e.getKey(), e.getValue().getValue());            }        }                return cookieNames;    }        // HttpHeaders        public MultivaluedMap<String, String> getRequestHeaders() {        return headers;    }    public List<String> getRequestHeader(String name) {        return headers.get(name);    }        public List<MediaType> getAcceptableMediaTypes() {        if (accept == null || headersModCount != headers.getModCount())            accept = new ArrayList<MediaType>(HttpHelper.getAccept(this));                            return accept;    }        public List<String> getAcceptableLanguages() {        throw new UnsupportedOperationException();    }        public MediaType getMediaType() {        if (contentType == null || headersModCount != headers.getModCount())            contentType = HttpHelper.getContentType(this);                return contentType;    }        public String getLanguage() {        return this.getRequestHeaders().getFirst("Content-Langauge");    }        public Map<String, Cookie> getCookies() {        if (cookies == null || headersModCount != headers.getModCount()) {            cookies = new HashMap<String, Cookie>();                        List<String> cl = getRequestHeaders().get("Cookie");            if (cl != null) {                for (String cookie : cl) {                    if (cookie != null)                        cookies.putAll(                                HttpHeaderFactory.createCookies(cookie));                }            }                    }                return cookies;    }            // Request    public Variant selectVariant(List<Variant> variants) {        if (variants == null || variants.isEmpty())             throw new IllegalArgumentException("The list of variants is null or empty");            // TODO mark the Vary header to be added to the response                return VariantSelector.selectVariant(this, variants);    }        public ResponseBuilder evaluatePreconditions(EntityTag eTag) {        ResponseBuilder r = evaluateIfMatch(eTag);        if (r == null)            r = evaluateIfNoneMatch(eTag);                return r;            }    public ResponseBuilder evaluatePreconditions(Date lastModified) {        long lastModifiedTime = lastModified.getTime();        ResponseBuilder r = evaluateIfUnmodifiedSince(lastModifiedTime);        if (r == null)            r = evaluateIfModifiedSince(lastModifiedTime);                return r;            }        public ResponseBuilder evaluatePreconditions(Date lastModified, EntityTag eTag) {        ResponseBuilder r = evaluateIfMatch(eTag);        if (r == null) {            long lastModifiedTime = lastModified.getTime();            r = evaluateIfUnmodifiedSince(lastModifiedTime);            if (r == null)                r = evaluateIfNoneMatch(eTag);            if (r == null)                r = evaluateIfModifiedSince(lastModifiedTime);        }                return r;            }            private ResponseBuilder evaluateIfMatch(EntityTag eTag) {        String ifMatchHeader = getRequestHeaders().getFirst("If-Match");        // TODO require support for eTag types        // Strong comparison of entity tags is required        if (ifMatchHeader != null &&                !ifMatchHeader.trim().equals("*") &&                !ifMatchHeader.contains(eTag.getValue())) {            // 412 Precondition Failed            return Responses.preconditionFailed();        }                return null;    }        private ResponseBuilder evaluateIfNoneMatch(EntityTag eTag) {        String ifNoneMatchHeader = getRequestHeaders().getFirst("If-None-Match");        if (ifNoneMatchHeader != null) {            // TODO require support for eTag types            // Weak entity tag comparisons can only be used            // with GET/HEAD            if (ifNoneMatchHeader.trim().equals("*") || ifNoneMatchHeader.contains(eTag.getValue())) {                String httpMethod = getHttpMethod();                if (httpMethod.equals("GET") || httpMethod.equals("HEAD")) {                    // 304 Not modified                    // TODO                    // Include cache related header fields                    // such as ETag                    return Response.notModified(eTag);                } else {                    // 412 Precondition Failed                    return Responses.preconditionFailed();                }            }        }                return null;    }        private ResponseBuilder evaluateIfUnmodifiedSince(long lastModified) {        String ifUnmodifiedSinceHeader = getRequestHeaders().getFirst("If-Unmodified-Since");        if (ifUnmodifiedSinceHeader != null) {            try {                long ifUnmodifiedSince = HttpHeaderReader.                        readDate(ifUnmodifiedSinceHeader).getTime() + 1000;                if (lastModified > ifUnmodifiedSince) {                    // 412 Precondition Failed                    return Responses.preconditionFailed();                }            } catch (ParseException ex) {                // Ignore the header if parsing error            }        }                return null;    }        private ResponseBuilder evaluateIfModifiedSince(long lastModified) {        String ifModifiedSinceHeader = getRequestHeaders().getFirst("If-Modified-Since");        if (ifModifiedSinceHeader != null) {            try {                // TODO round up if modified since or round down last modified                long ifModifiedSince = HttpHeaderReader.                        readDate(ifModifiedSinceHeader).getTime() + 1000;                if (ifModifiedSince  > lastModified) {                    // 304 Not modified                    return Responses.notModified();                }            } catch (ParseException ex) {                // Ignore the header if parsing error            }        }                return null;    }        // SecurityContext        public Principal getUserPrincipal() {        throw new UnsupportedOperationException();    }        public boolean isUserInRole(String role) {        throw new UnsupportedOperationException();    }        public boolean isSecure() {        throw new UnsupportedOperationException();    }        public String getAuthenticationScheme() {        throw new UnsupportedOperationException();    }}

⌨️ 快捷键说明

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