Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit d3b0e84

Browse files
author
Adam Lindenthal
committed
Fix for JERSEY-2520 - Jersey 2.x app missing */* mime type in WADL
Change-Id: Ibf23967be15c25416cf855aa4573001a0ad7bd93
1 parent 4dd9a31 commit d3b0e84

File tree

2 files changed

+116
-4
lines changed

2 files changed

+116
-4
lines changed

core-server/src/main/java/org/glassfish/jersey/server/wadl/internal/WadlGeneratorImpl.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2010-2013 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2010-2014 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -204,9 +204,12 @@ public Resource createResource(org.glassfish.jersey.server.model.Resource resour
204204
public List<Response> createResponses(org.glassfish.jersey.server.model.Resource r, ResourceMethod m) {
205205
final Response response = new Response();
206206

207-
for (MediaType mediaType : m.getProducedTypes()) {
208-
if (!MediaType.WILDCARD_TYPE.equals(mediaType)
209-
|| !hasEmptyProducibleMediaTypeSet(m)) {
207+
// add mediaType="*/*" in case that no mediaType was specified
208+
if (hasEmptyProducibleMediaTypeSet(m)) {
209+
Representation wadlRepresentation = createResponseRepresentation(r, m, MediaType.WILDCARD_TYPE);
210+
response.getRepresentation().add(wadlRepresentation);
211+
} else {
212+
for (MediaType mediaType : m.getProducedTypes()) {
210213
Representation wadlRepresentation = createResponseRepresentation(r, m, mediaType);
211214
response.getRepresentation().add(wadlRepresentation);
212215
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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.server.wadl;
41+
42+
import java.io.IOException;
43+
import java.io.StringReader;
44+
45+
import javax.ws.rs.GET;
46+
import javax.ws.rs.Path;
47+
import javax.ws.rs.Produces;
48+
import javax.ws.rs.client.WebTarget;
49+
import javax.ws.rs.core.Application;
50+
import javax.xml.parsers.DocumentBuilderFactory;
51+
import javax.xml.parsers.ParserConfigurationException;
52+
import javax.xml.xpath.XPath;
53+
import javax.xml.xpath.XPathExpressionException;
54+
import javax.xml.xpath.XPathFactory;
55+
56+
import org.glassfish.jersey.server.ResourceConfig;
57+
import org.glassfish.jersey.test.JerseyTest;
58+
59+
import org.junit.Test;
60+
import org.w3c.dom.Document;
61+
import org.xml.sax.InputSource;
62+
import org.xml.sax.SAXException;
63+
64+
import static org.junit.Assert.assertEquals;
65+
66+
/**
67+
* Tests, that Jersey returns wildcard mediaType in case no response representation was specified.
68+
*
69+
* @author Adam Lindenthal (adam.lindenthal at oracle.com)
70+
*/
71+
public class WadlEmptyMediaTypeTest extends JerseyTest {
72+
73+
@Path("test")
74+
public static class WadlEmptyMediaTypeTestResource {
75+
@Path("getEmpty")
76+
@GET
77+
public String getEmpty() {
78+
return "No @Produces annotation";
79+
}
80+
81+
@Path("getText")
82+
@Produces("text/plain")
83+
@GET
84+
public String getText() {
85+
return "Produces text/plain";
86+
}
87+
}
88+
89+
@Override
90+
protected Application configure() {
91+
return new ResourceConfig(WadlEmptyMediaTypeTestResource.class);
92+
}
93+
94+
@Test
95+
public void testOverride() throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {
96+
WebTarget target = target("/application.wadl");
97+
String wadl = target.request().get(String.class);
98+
99+
InputSource is = new InputSource(new StringReader(wadl));
100+
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
101+
102+
XPath xpath = XPathFactory.newInstance().newXPath();
103+
String val = xpath.evaluate("//method[@id='getEmpty']/response/representation/@mediaType", document);
104+
assertEquals("*/*", val);
105+
106+
val = xpath.evaluate("//method[@id='getText']/response/representation/@mediaType", document);
107+
assertEquals("text/plain", val);
108+
}
109+
}

0 commit comments

Comments
 (0)