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.
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2012-2013 Oracle and/or its affiliates. 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
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
* or packager/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 packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [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 org.glassfish.jersey.message.internal;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.net.URI;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.core.Cookie;
import javax.ws.rs.core.EntityTag;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Link;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.NewCookie;
import javax.ws.rs.ext.RuntimeDelegate;
import org.glassfish.jersey.internal.LocalizationMessages;
import org.glassfish.jersey.internal.ProcessingException;
import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
/**
* Base outbound message context implementation.
*
* @author Marek Potociar (marek.potociar at oracle.com)
*/
public class OutboundMessageContext {
private static final Annotation[] EMPTY_ANNOTATIONS = new Annotation[0];
private final MultivaluedMap headers;
private final CommittingOutputStream rootStream;
private Object entity;
private GenericType> entityType;
private Annotation[] entityAnnotations = EMPTY_ANNOTATIONS;
private OutputStream entityStream;
/**
* Output stream provider that provides access to the entity output stream.
*/
public static interface StreamProvider {
/**
* Get the output stream.
*
* The method is called once as part of a "commit" operation immediately after
* the {@link #commit()} method has been invoked.
*
* @return the adapted output stream.
* @throws java.io.IOException in case of an IO error.
*/
public OutputStream getOutputStream() throws IOException;
/**
* Perform the commit functionality.
*
* The method is called once as part of a "commit" operation before the first byte
* is written to the provided output stream.
*
* @throws java.io.IOException in case of an IO error.
*/
public void commit() throws IOException;
}
/**
* Create new outbound message context.
*/
public OutboundMessageContext() {
this.headers = HeadersFactory.createOutbound();
this.rootStream = new CommittingOutputStream();
this.entityStream = rootStream;
}
/**
* Create new outbound message context copying the content
* of another context.
*
* @param original the original outbound message context.
*/
public OutboundMessageContext(OutboundMessageContext original) {
this.headers = HeadersFactory.createOutbound();
this.headers.putAll(original.headers);
this.rootStream = new CommittingOutputStream();
this.entityStream = rootStream;
this.entity = original.entity;
this.entityType = original.entityType;
this.entityAnnotations = original.entityAnnotations;
}
/**
* Replace all headers.
*
* @param headers new headers.
*/
public void replaceHeaders(MultivaluedMap headers) {
getHeaders().clear();
if (headers != null) {
getHeaders().putAll(headers);
}
}
/**
* Get a multi-valued map representing outbound message headers with their values converted
* to strings.
*
* @return multi-valued map of outbound message header names to their string-converted values.
*/
public MultivaluedMap getStringHeaders() {
return HeadersFactory.asStringHeaders(headers);
}
/**
* Get a message header as a single string value.
*
* Each single header value is converted to String using a
* {@link javax.ws.rs.ext.RuntimeDelegate.HeaderDelegate} if one is available
* via {@link javax.ws.rs.ext.RuntimeDelegate#createHeaderDelegate(java.lang.Class)}
* for the header value class or using its {@code toString} method if a header
* delegate is not available.
*
* @param name the message header.
* @return the message header value. If the message header is not present then
* {@code null} is returned. If the message header is present but has no
* value then the empty string is returned. If the message header is present
* more than once then the values of joined together and separated by a ','
* character.
*/
public String getHeaderString(String name) {
return HeadersFactory.asHeaderString(headers.get(name), RuntimeDelegate.getInstance());
}
/**
* Get a single typed header value.
*
* @param header value type.
* @param name header name.
* @param valueType header value class.
* @param converter from string conversion function. Is expected to throw {@link ProcessingException}
* if conversion fails.
* @param convertNull if {@code true} this method calls the provided converter even for {@code null}. Otherwise this
* method returns the {@code null} without calling the converter.
* @return value of the header, or (possibly converted) {@code null} if not present.
*/
private T singleHeader(String name, Class valueType, Function converter, boolean convertNull) {
final List