Skip to content

Commit 6693a9c

Browse files
author
Arun Gupta
committed
Adding a new sample to show client-side content negotiation
1 parent 8df909a commit 6693a9c

7 files changed

Lines changed: 436 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project-shared-configuration>
3+
<!--
4+
This file contains additional configuration written by modules in the NetBeans IDE.
5+
The configuration is intended to be shared among all the users of project and
6+
therefore it is assumed to be part of version control checkout.
7+
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
8+
-->
9+
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
10+
<!--
11+
Properties that influence various parts of the IDE, especially code formatting and the like.
12+
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
13+
That way multiple projects can share the same settings (useful for formatting rules for example).
14+
Any value defined here will override the pom.xml file value but is only applicable to the current project.
15+
-->
16+
<org-netbeans-modules-maven-jaxws.rest_2e_config_2e_type>ide</org-netbeans-modules-maven-jaxws.rest_2e_config_2e_type>
17+
<org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_deploy_2e_server>gfv3ee6</org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_deploy_2e_server>
18+
</properties>
19+
</project-shared-configuration>

jaxrs/client-negotiation/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.javaee7.jaxrs</groupId>
6+
<artifactId>jaxrs-samples</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
11+
<groupId>org.javaee7.jaxrs</groupId>
12+
<artifactId>client-negotiation</artifactId>
13+
<version>1.0-SNAPSHOT</version>
14+
<packaging>war</packaging>
15+
</project>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 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://glassfish.dev.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.javaee7.jaxrs.client.negotiation;
41+
42+
import java.util.Set;
43+
import javax.ws.rs.ApplicationPath;
44+
import javax.ws.rs.core.Application;
45+
46+
/**
47+
* @author Arun Gupta
48+
*/
49+
@ApplicationPath("webresources")
50+
public class MyApplication extends Application {
51+
52+
@Override
53+
public Set<Class<?>> getClasses() {
54+
Set<Class<?>> resources = new java.util.HashSet<>();
55+
resources.add(MyResource.class);
56+
return resources;
57+
}
58+
59+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 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://glassfish.dev.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.javaee7.jaxrs.client.negotiation;
41+
42+
import javax.ws.rs.GET;
43+
import javax.ws.rs.Path;
44+
import javax.ws.rs.Produces;
45+
46+
/**
47+
* @author Arun Gupta
48+
*/
49+
@Path("persons")
50+
public class MyResource {
51+
@GET
52+
@Produces({"application/xml", "application/json"})
53+
public Person[] getList() {
54+
Person[] list = new Person[3];
55+
list[0] = new Person("Penny", 1);
56+
list[1] = new Person("Howard", 2);
57+
list[2] = new Person("Sheldon", 3);
58+
59+
return list;
60+
}
61+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 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://glassfish.dev.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.javaee7.jaxrs.client.negotiation;
41+
42+
import javax.xml.bind.annotation.XmlRootElement;
43+
44+
/**
45+
* @author Arun Gupta
46+
*/
47+
@XmlRootElement
48+
public class Person {
49+
private String name;
50+
private int age;
51+
52+
public Person() {
53+
}
54+
55+
public Person(String name, int age) {
56+
this.name = name;
57+
this.age = age;
58+
}
59+
60+
public String getName() {
61+
return name;
62+
}
63+
64+
public void setName(String name) {
65+
this.name = name;
66+
}
67+
68+
public int getAge() {
69+
return age;
70+
}
71+
72+
public void setAge(int age) {
73+
this.age = age;
74+
}
75+
76+
@Override
77+
public String toString() {
78+
return name + "(" + age + ")";
79+
}
80+
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 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://glassfish.dev.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.javaee7.jaxrs.client.negotiation;
41+
42+
import java.io.IOException;
43+
import java.io.PrintWriter;
44+
import javax.servlet.ServletException;
45+
import javax.servlet.annotation.WebServlet;
46+
import javax.servlet.http.HttpServlet;
47+
import javax.servlet.http.HttpServletRequest;
48+
import javax.servlet.http.HttpServletResponse;
49+
import javax.ws.rs.client.Client;
50+
import javax.ws.rs.client.ClientBuilder;
51+
import javax.ws.rs.client.WebTarget;
52+
import org.glassfish.jersey.filter.LoggingFilter;
53+
54+
/**
55+
* @author Arun Gupta
56+
*/
57+
@WebServlet(urlPatterns = {"/TestServlet"})
58+
public class TestServlet extends HttpServlet {
59+
60+
/**
61+
* Processes requests for both HTTP
62+
* <code>GET</code> and
63+
* <code>POST</code> methods.
64+
*
65+
* @param request servlet request
66+
* @param response servlet response
67+
* @throws ServletException if a servlet-specific error occurs
68+
* @throws IOException if an I/O error occurs
69+
*/
70+
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
71+
throws ServletException, IOException {
72+
response.setContentType("text/html;charset=UTF-8");
73+
PrintWriter out = response.getWriter();
74+
out.println("<html>");
75+
out.println("<head>");
76+
out.println("<title>Client-side Content Negotiation</title>");
77+
out.println("</head>");
78+
out.println("<body>");
79+
out.println("<h1>Client-side Content Negotiation</h1>");
80+
out.println("Initializing client...<br>");
81+
Client client = ClientBuilder.newClient();
82+
WebTarget target = client.
83+
register(LoggingFilter.class)
84+
.target("http://"
85+
+ request.getServerName()
86+
+ ":"
87+
+ request.getServerPort()
88+
+ request.getContextPath()
89+
+ "/webresources/persons");
90+
91+
// GET
92+
out.print("<br>GETTing application/xml ...<br>");
93+
String string = target.request("application/xml").get(String.class);
94+
out.format("GOT the representation: " + string);
95+
96+
// GET
97+
out.print("<br><br>GETTing application/json ...<br>");
98+
string = target.request("application/json").get(String.class);
99+
out.format("GOT the representation: " + string);
100+
101+
out.println("<br><br>... done.<br>");
102+
103+
out.println("</body>");
104+
out.println("</html>");
105+
}
106+
107+
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
108+
/**
109+
* Handles the HTTP
110+
* <code>GET</code> method.
111+
*
112+
* @param request servlet request
113+
* @param response servlet response
114+
* @throws ServletException if a servlet-specific error occurs
115+
* @throws IOException if an I/O error occurs
116+
*/
117+
@Override
118+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
119+
throws ServletException, IOException {
120+
processRequest(request, response);
121+
}
122+
123+
/**
124+
* Handles the HTTP
125+
* <code>POST</code> method.
126+
*
127+
* @param request servlet request
128+
* @param response servlet response
129+
* @throws ServletException if a servlet-specific error occurs
130+
* @throws IOException if an I/O error occurs
131+
*/
132+
@Override
133+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
134+
throws ServletException, IOException {
135+
processRequest(request, response);
136+
}
137+
138+
/**
139+
* Returns a short description of the servlet.
140+
*
141+
* @return a String containing servlet description
142+
*/
143+
@Override
144+
public String getServletInfo() {
145+
return "Short description";
146+
}// </editor-fold>
147+
}

0 commit comments

Comments
 (0)