org.apache.juneau.encoders.EncoderGroup Maven / Gradle / Ivy
// ***************************************************************************************************************************
// * 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.juneau.encoders;
import java.util.*;
import java.util.concurrent.*;
import org.apache.juneau.http.*;
/**
* Represents the group of {@link Encoder encoders} keyed by codings.
*
* Description:
*
* Maintains a set of encoders and the codings that they can handle.
*
*
* The {@link #getEncoderMatch(String)} and {@link #getEncoder(String)} methods are then used to find appropriate
* encoders for specific Accept-Encoding
and Content-Encoding
header values.
*
*
Match ordering
*
* Encoders are matched against Accept-Encoding
strings in the order they exist in this group.
*
*
* Adding new entries will cause the entries to be prepended to the group.
* This allows for previous encoders to be overridden through subsequent calls.
*
*
* For example, calling groupBuilder.append(E1.class ,E2.class ).append(E3.class ,
* E4.class )
will result in the order E3, E4, E1, E2
.
*
*
Example:
*
* // Create an encoder group with support for gzip compression.
* EncoderGroup g = new EncoderGroupBuilder().append(GzipEncoder.class ).build();
*
* // Should return "gzip"
* String matchedCoding = g.findMatch("compress;q=1.0, gzip;q=0.8, identity;q=0.5, *;q=0" );
*
* // Get the encoder
* IEncoder encoder = g.getEncoder(matchedCoding);
*
*/
public final class EncoderGroup {
// Maps Accept-Encoding headers to matching encoders.
private final ConcurrentHashMap cache = new ConcurrentHashMap();
private final String[] encodings;
private final List encodingsList;
private final Encoder[] encodingsEncoders;
private final List encoders;
/**
* Constructor
*
* @param encoders The encoders to add to this group.
*/
public EncoderGroup(Encoder[] encoders) {
this.encoders = Collections.unmodifiableList(new ArrayList(Arrays.asList(encoders)));
List lc = new ArrayList();
List l = new ArrayList();
for (Encoder e : encoders) {
for (String c: e.getCodings()) {
lc.add(c);
l.add(e);
}
}
this.encodings = lc.toArray(new String[lc.size()]);
this.encodingsList = Collections.unmodifiableList(lc);
this.encodingsEncoders = l.toArray(new Encoder[l.size()]);
}
/**
* Returns the coding string for the matching encoder that can handle the specified Accept-Encoding
* or Content-Encoding
header value.
*
*
* Returns null if no encoders can handle it.
*
*
* This method is fully compliant with the RFC2616/14.3 and 14.11 specifications.
*
* @param acceptEncoding The Accept-Encoding
or Content-Encoding
value.
* @return The coding value (e.g. "gzip" ).
*/
public EncoderMatch getEncoderMatch(String acceptEncoding) {
EncoderMatch em = cache.get(acceptEncoding);
if (em != null)
return em;
AcceptEncoding ae = AcceptEncoding.forString(acceptEncoding);
int match = ae.findMatch(encodings);
if (match >= 0) {
em = new EncoderMatch(encodings[match], encodingsEncoders[match]);
cache.putIfAbsent(acceptEncoding, em);
}
return cache.get(acceptEncoding);
}
/**
* Returns the encoder registered with the specified coding (e.g. "gzip" ).
*
* @param encoding The coding string.
* @return The encoder, or null if encoder isn't registered with that coding.
*/
public Encoder getEncoder(String encoding) {
EncoderMatch em = getEncoderMatch(encoding);
return (em == null ? null : em.getEncoder());
}
/**
* Returns the set of codings supported by all encoders in this group.
*
* @return An unmodifiable list of codings supported by all encoders in this group. Never null .
*/
public List getSupportedEncodings() {
return encodingsList;
}
/**
* Returns the encoders in this group.
*
* @return An unmodifiable list of encoders in this group.
*/
public List getEncoders() {
return encoders;
}
}