PageRenderTime 34ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/core-client/src/main/java/org/glassfish/jersey/client/filter/EncodingFilter.java

http://github.com/jersey/jersey
Java | 116 lines | 52 code | 10 blank | 54 comment | 11 complexity | 871a198c87470cfdb8cd12fb77f6224a MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright (c) 2012-2017 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * The contents of this file are subject to the terms of either the GNU
  7. * General Public License Version 2 only ("GPL") or the Common Development
  8. * and Distribution License("CDDL") (collectively, the "License"). You
  9. * may not use this file except in compliance with the License. You can
  10. * obtain a copy of the License at
  11. * https://oss.oracle.com/licenses/CDDL+GPL-1.1
  12. * or LICENSE.txt. See the License for the specific
  13. * language governing permissions and limitations under the License.
  14. *
  15. * When distributing the software, include this License Header Notice in each
  16. * file and include the License file at LICENSE.txt.
  17. *
  18. * GPL Classpath Exception:
  19. * Oracle designates this particular file as subject to the "Classpath"
  20. * exception as provided by Oracle in the GPL Version 2 section of the License
  21. * file that accompanied this code.
  22. *
  23. * Modifications:
  24. * If applicable, add the following below the License Header, with the fields
  25. * enclosed by brackets [] replaced by your own identifying information:
  26. * "Portions Copyright [year] [name of copyright owner]"
  27. *
  28. * Contributor(s):
  29. * If you wish your version of this file to be governed by only the CDDL or
  30. * only the GPL Version 2, indicate your decision by adding "[Contributor]
  31. * elects to include this software in this distribution under the [CDDL or GPL
  32. * Version 2] license." If you don't indicate a single choice of license, a
  33. * recipient has the option to distribute your version of this file under
  34. * either the CDDL, the GPL Version 2 or to extend the choice of license to
  35. * its licensees as provided above. However, if you add GPL Version 2 code
  36. * and therefore, elected the GPL Version 2 license, then the option applies
  37. * only if the new code is made subject to such option by the copyright
  38. * holder.
  39. */
  40. package org.glassfish.jersey.client.filter;
  41. import java.io.IOException;
  42. import java.util.ArrayList;
  43. import java.util.List;
  44. import java.util.SortedSet;
  45. import java.util.TreeSet;
  46. import java.util.logging.Logger;
  47. import javax.ws.rs.client.ClientRequestContext;
  48. import javax.ws.rs.client.ClientRequestFilter;
  49. import javax.ws.rs.core.HttpHeaders;
  50. import javax.inject.Inject;
  51. import org.glassfish.jersey.client.ClientConfig;
  52. import org.glassfish.jersey.client.ClientProperties;
  53. import org.glassfish.jersey.client.internal.LocalizationMessages;
  54. import org.glassfish.jersey.internal.inject.InjectionManager;
  55. import org.glassfish.jersey.spi.ContentEncoder;
  56. /**
  57. * Client filter adding support for {@link org.glassfish.jersey.spi.ContentEncoder content encoding}. The filter adds
  58. * list of supported encodings to the Accept-Header values.
  59. * Supported encodings are determined by looking
  60. * up all the {@link org.glassfish.jersey.spi.ContentEncoder} implementations registered in the corresponding
  61. * {@link ClientConfig client configuration}.
  62. * <p>
  63. * If {@link ClientProperties#USE_ENCODING} client property is set, the filter will add Content-Encoding header with
  64. * the value of the property, unless Content-Encoding header has already been set.
  65. * </p>
  66. *
  67. * @author Martin Matula
  68. */
  69. public final class EncodingFilter implements ClientRequestFilter {
  70. @Inject
  71. private InjectionManager injectionManager;
  72. private volatile List<Object> supportedEncodings = null;
  73. @Override
  74. public void filter(ClientRequestContext request) throws IOException {
  75. if (getSupportedEncodings().isEmpty()) {
  76. return;
  77. }
  78. request.getHeaders().addAll(HttpHeaders.ACCEPT_ENCODING, getSupportedEncodings());
  79. String useEncoding = (String) request.getConfiguration().getProperty(ClientProperties.USE_ENCODING);
  80. if (useEncoding != null) {
  81. if (!getSupportedEncodings().contains(useEncoding)) {
  82. Logger.getLogger(getClass().getName()).warning(LocalizationMessages.USE_ENCODING_IGNORED(
  83. ClientProperties.USE_ENCODING, useEncoding, getSupportedEncodings()));
  84. } else {
  85. if (request.hasEntity()) { // don't add Content-Encoding header for requests with no entity
  86. if (request.getHeaders().getFirst(HttpHeaders.CONTENT_ENCODING) == null) {
  87. request.getHeaders().putSingle(HttpHeaders.CONTENT_ENCODING, useEncoding);
  88. }
  89. }
  90. }
  91. }
  92. }
  93. List<Object> getSupportedEncodings() {
  94. // no need for synchronization - in case of a race condition, the property
  95. // may be set twice, but it does not break anything
  96. if (supportedEncodings == null) {
  97. SortedSet<String> se = new TreeSet<>();
  98. List<ContentEncoder> encoders = injectionManager.getAllInstances(ContentEncoder.class);
  99. for (ContentEncoder encoder : encoders) {
  100. se.addAll(encoder.getSupportedEncodings());
  101. }
  102. supportedEncodings = new ArrayList<>(se);
  103. }
  104. return supportedEncodings;
  105. }
  106. }