|
Apache CXF example source code file (GZIPOutInterceptor.java)
The Apache CXF GZIPOutInterceptor.java source code/** * 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.transport.http.gzip; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.ResourceBundle; import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.zip.GZIPOutputStream; import org.apache.cxf.common.i18n.BundleUtils; import org.apache.cxf.common.logging.LogUtils; import org.apache.cxf.helpers.CastUtils; import org.apache.cxf.helpers.HttpHeaderHelper; import org.apache.cxf.interceptor.Fault; import org.apache.cxf.interceptor.MessageSenderInterceptor; import org.apache.cxf.io.AbstractThresholdOutputStream; import org.apache.cxf.message.Exchange; import org.apache.cxf.message.Message; import org.apache.cxf.phase.AbstractPhaseInterceptor; import org.apache.cxf.phase.Phase; /** * CXF interceptor that compresses outgoing messages using gzip and sets the * HTTP Content-Encoding header appropriately. An instance of this class should * be added as an out interceptor on clients that need to talk to a service that * accepts gzip-encoded requests or on a service that wants to be able to return * compressed responses. In server mode, the interceptor only compresses * responses if the client indicated (via an Accept-Encoding header on the * request) that it can understand them. To handle gzip-encoded input messages, * see {@link GZIPInInterceptor}. This interceptor supports a compression * {@link #threshold} (default 1kB) - messages smaller than this threshold will * not be compressed. To force compression of all messages, set the threshold to * 0. This class was originally based on one of the CXF samples * (configuration_interceptor). * * @author Ian Roberts (i.roberts@dcs.shef.ac.uk) */ public class GZIPOutInterceptor extends AbstractPhaseInterceptor<Message> { /** * Enum giving the possible values for whether we should gzip a particular * message. */ public static enum UseGzip { NO, YES, FORCE } /** * Key under which we store the original output stream on the message, for * use by the ending interceptor. */ public static final String ORIGINAL_OUTPUT_STREAM_KEY = GZIPOutInterceptor.class.getName() + ".originalOutputStream"; /** * Key under which we store an indication of whether compression is * permitted or required, for use by the ending interceptor. */ public static final String USE_GZIP_KEY = GZIPOutInterceptor.class.getName() + ".useGzip"; /** * Key under which we store the name which should be used for the * content-encoding of the outgoing message. Typically "gzip" but may be * "x-gzip" if we are processing a response message and this is the name * given by the client in Accept-Encoding. */ public static final String GZIP_ENCODING_KEY = GZIPOutInterceptor.class.getName() + ".gzipEncoding"; private static final ResourceBundle BUNDLE = BundleUtils.getBundle(GZIPOutInterceptor.class); private static final Logger LOG = LogUtils.getL7dLogger(GZIPOutInterceptor.class); /** * Compression threshold in bytes - messages smaller than this will not be * compressed. */ private int threshold = 1024; public GZIPOutInterceptor() { super(Phase.PREPARE_SEND); addAfter(MessageSenderInterceptor.class.getName()); } public void setThreshold(int threshold) { this.threshold = threshold; } public int getThreshold() { return threshold; } public void handleMessage(Message message) throws Fault { UseGzip use = gzipPermitted(message); if (use != UseGzip.NO) { // remember the original output stream, we will write compressed // data to this later OutputStream os = message.getContent(OutputStream.class); if (os == null) { return; } message.put(ORIGINAL_OUTPUT_STREAM_KEY, os); message.put(USE_GZIP_KEY, use); // new stream to cache the message GZipThresholdOutputStream cs = new GZipThresholdOutputStream(threshold, os, use == UseGzip.FORCE, message); message.setContent(OutputStream.class, cs); } } /** * Checks whether we can, cannot or must use gzip compression on this output * message. Gzip is always permitted if the message is a client request. If * the message is a server response we check the Accept-Encoding header of * the corresponding request message - with no Accept-Encoding we assume * that gzip is not permitted. For the full gory details, see <a * href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3">section * 14.3 of RFC 2616</a> (HTTP 1.1). * * @param message the outgoing message. * @return whether to attempt gzip compression for this message. * @throws Fault if the Accept-Encoding header does not allow any encoding * that we can support (identity, gzip or x-gzip). */ private UseGzip gzipPermitted(Message message) throws Fault { UseGzip permitted = UseGzip.NO; if (Boolean.TRUE.equals(message.get(Message.REQUESTOR_ROLE))) { LOG.fine("Requestor role, so gzip enabled"); permitted = UseGzip.YES; message.put(GZIP_ENCODING_KEY, "gzip"); addHeader(message, "Accept-Encoding", "gzip;q=1.0, identity; q=0.5, *;q=0"); } else { LOG.fine("Response role, checking accept-encoding"); Exchange exchange = message.getExchange(); Message request = exchange.getInMessage(); Map<String, List Other Apache CXF examples (source code examples)Here is a short list of links related to this Apache CXF GZIPOutInterceptor.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.