1+ /*
2+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+ *
4+ * Copyright (c) 2014 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+ * http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+ * or packager/legal/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 packager/legal/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 .tests .e2e .json ;
41+
42+ import java .util .logging .Logger ;
43+
44+ import javax .ws .rs .GET ;
45+ import javax .ws .rs .Path ;
46+ import javax .ws .rs .Produces ;
47+ import javax .ws .rs .core .Context ;
48+ import javax .ws .rs .core .HttpHeaders ;
49+ import javax .ws .rs .core .MultivaluedMap ;
50+ import javax .ws .rs .core .Response ;
51+
52+ import org .glassfish .jersey .jackson .JacksonFeature ;
53+ import org .glassfish .jersey .message .DeflateEncoder ;
54+ import org .glassfish .jersey .message .GZipEncoder ;
55+ import org .glassfish .jersey .server .JSONP ;
56+ import org .glassfish .jersey .server .ResourceConfig ;
57+ import org .glassfish .jersey .server .filter .EncodingFilter ;
58+ import org .glassfish .jersey .test .JerseyTest ;
59+ import org .glassfish .jersey .test .TestProperties ;
60+
61+ import org .junit .Test ;
62+
63+ import static org .junit .Assert .assertEquals ;
64+ import static org .junit .Assert .assertTrue ;
65+
66+ /**
67+ * Tests, that JSONP callback wrapping takes places before the eventual entity compression.
68+ *
69+ * See https://java.net/jira/browse/JERSEY-2524 for the original issue description.
70+ *
71+ * @author Adam Lindenthal (adam.lindenthal at oracle.com)
72+ */
73+ public class JsonWithPaddingEncodingFilterTest extends JerseyTest {
74+ private static final Logger LOGGER = Logger .getLogger (JsonWithPaddingEncodingFilterTest .class .getName ());
75+
76+ @ Override
77+ protected ResourceConfig configure () {
78+ enable (TestProperties .LOG_TRAFFIC );
79+ return new ResourceConfig (MyResource .class )
80+ .register (JacksonFeature .class )
81+ .register (EncodingFilter .class )
82+ .register (GZipEncoder .class )
83+ .register (DeflateEncoder .class );
84+ }
85+
86+ @ Path ("rest" )
87+ public static class MyResource {
88+ @ GET
89+ @ Path ("jsonp" )
90+ @ JSONP (queryParam = JSONP .DEFAULT_QUERY )
91+ @ Produces ("application/x-javascript" )
92+ public Message getHelloJsonP (@ Context HttpHeaders headers ) {
93+ MultivaluedMap <String , String > headerParams = headers .getRequestHeaders ();
94+ for (String key : headerParams .keySet ()) {
95+ System .out .println (key + ": " );
96+ for (String value : headerParams .get (key )) {
97+ System .out .print (value + ", " );
98+ }
99+ System .out .println ("\b \b " );
100+ }
101+ return new Message ("Hello world JsonP!" , "English" );
102+ }
103+ }
104+
105+ public static class Message {
106+ private String greeting ;
107+ private String language ;
108+
109+ public Message (String greeting , String language ) {
110+ this .greeting = greeting ;
111+ this .language = language ;
112+ }
113+
114+ public String getGreeting () {
115+ return greeting ;
116+ }
117+
118+ public String getLanguage () {
119+ return language ;
120+ }
121+ }
122+
123+ @ Test
124+ public void testCorrectGzipDecoding () {
125+ final Response response = target ().path ("rest/jsonp" ).queryParam ("__callback" , "dialog" )
126+ .register (GZipEncoder .class ).request ("application/x-javascript" )
127+ .header ("Accept-Encoding" , "gzip" ).get ();
128+
129+ String result = response .readEntity (String .class );
130+ assertEquals ("gzip" , response .getHeaders ().getFirst ("Content-Encoding" ));
131+
132+ assertTrue (result .startsWith ("dialog(" ));
133+ assertTrue (result .contains ("Hello world JsonP!" ));
134+ assertTrue (result .contains ("English" ));
135+ }
136+
137+ @ Test
138+ public void testCorrectDeflateDecoding () {
139+ final Response response = target ().path ("rest/jsonp" ).queryParam ("__callback" , "dialog" )
140+ .register (DeflateEncoder .class ).request ("application/x-javascript" )
141+ .header ("Accept-Encoding" , "deflate" ).get ();
142+
143+ String result = response .readEntity (String .class );
144+ assertEquals ("deflate" , response .getHeaders ().getFirst ("Content-Encoding" ));
145+
146+ assertTrue (result .startsWith ("dialog(" ));
147+ assertTrue (result .contains ("Hello world JsonP!" ));
148+ assertTrue (result .contains ("English" ));
149+ }
150+ }
0 commit comments