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.wink.server.internal.handlers;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Properties;
import java.util.Map.Entry;
import javax.activation.CommandMap;
import javax.activation.DataContentHandler;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Providers;
import javax.ws.rs.ext.RuntimeDelegate;
import javax.ws.rs.ext.RuntimeDelegate.HeaderDelegate;
import org.apache.wink.common.internal.MultivaluedMapImpl;
import org.apache.wink.common.internal.i18n.Messages;
import org.apache.wink.common.internal.runtime.RuntimeContextTLS;
import org.apache.wink.common.utils.ProviderUtils;
import org.apache.wink.common.utils.ProviderUtils.PROVIDER_EXCEPTION_ORIGINATOR;
import org.apache.wink.server.handlers.AbstractHandler;
import org.apache.wink.server.handlers.MessageContext;
import org.apache.wink.server.internal.contexts.RequestImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FlushResultHandler extends AbstractHandler {
private static final Logger logger =
LoggerFactory
.getLogger(FlushResultHandler.class);
private static final RuntimeDelegate runtimeDelegate = RuntimeDelegate.getInstance();
@SuppressWarnings("unchecked")
public void handleResponse(MessageContext context) throws Throwable {
// assert status code is valid
int statusCode = context.getResponseStatusCode();
if (statusCode < 0) {
logger.trace("Status code was not set. Nothing to do."); //$NON-NLS-1$
return;
}
// assert response is not committed
final HttpServletResponse httpResponse = context.getAttribute(HttpServletResponse.class);
if (httpResponse.isCommitted()) {
logger.trace("The response is already committed. Nothing to do."); //$NON-NLS-1$
return;
}
// set the status code
logger.trace("Response status code set to: {}", statusCode); //$NON-NLS-1$
httpResponse.setStatus(statusCode);
// get the entity
Object entity = context.getResponseEntity();
boolean isOriginalEntityResponseObj = false;
// extract the entity and headers from the response (if it is a
// response)
MultivaluedMap httpHeaders = null;
if (entity instanceof Response) {
Response response = (Response)entity;
entity = response.getEntity();
httpHeaders = response.getMetadata();
isOriginalEntityResponseObj = true;
}
// prepare the entity to write, its class and generic type
Type genericType = null;
Annotation[] declaredAnnotations = null;
SearchResult searchResult = context.getAttribute(SearchResult.class);
if (searchResult != null && searchResult.isFound()) {
Method reflectionMethod = searchResult.getMethod().getMetadata().getReflectionMethod();
genericType = reflectionMethod.getGenericReturnType();
declaredAnnotations = reflectionMethod.getDeclaredAnnotations();
} else {
declaredAnnotations = new Annotation[0];
}
Class> rawType = null;
if (entity instanceof GenericEntity) {
GenericEntity> genericEntity = (GenericEntity>)entity;
entity = genericEntity.getEntity();
rawType = genericEntity.getRawType();
genericType = genericEntity.getType();
} else {
rawType = (entity != null ? entity.getClass() : null);
if (isOriginalEntityResponseObj) {
genericType = rawType;
} else {
genericType = (genericType != null ? genericType : rawType);
}
}
if (httpHeaders == null) {
httpHeaders = new MultivaluedMapImpl();
}
// we're done if the actual entity is null
if (entity == null) {
logger.trace("No entity so writing only the headers"); //$NON-NLS-1$
flushHeaders(httpResponse, httpHeaders);
return;
}
// we have an entity, get the response media type
// the actual writing will take places in the flush
MediaType responseMediaType = context.getResponseMediaType();
// get the provider to write the entity
Providers providers = context.getProviders();
MessageBodyWriter