Skip to content

Commit 0d875de

Browse files
committed
Add HttpHeader and UriInfo resources
0 parents  commit 0d875de

File tree

6 files changed

+221
-0
lines changed

6 files changed

+221
-0
lines changed

jax-rs-context/pom.xml

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<artifactId>jax-rs-context</artifactId>
5+
<packaging>war</packaging>
6+
7+
<parent>
8+
<groupId>com.readlearncode</groupId>
9+
<artifactId>article-code-examples</artifactId>
10+
<version>1.0</version>
11+
</parent>
12+
13+
<name>rest-server</name>
14+
15+
<prerequisites>
16+
<maven>3.2.1</maven>
17+
</prerequisites>
18+
19+
<properties>
20+
<java.version>1.8</java.version>
21+
<javaee-api.version>7.0</javaee-api.version>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
</properties>
24+
25+
<dependencies>
26+
<dependency>
27+
<groupId>javax</groupId>
28+
<artifactId>javaee-api</artifactId>
29+
<version>${javaee-api.version}</version>
30+
</dependency>
31+
</dependencies>
32+
33+
34+
<build>
35+
<plugins>
36+
<plugin>
37+
<groupId>org.apache.maven.plugins</groupId>
38+
<artifactId>maven-compiler-plugin</artifactId>
39+
<version>3.6.1</version>
40+
<configuration>
41+
<source>1.8</source>
42+
<target>1.8</target>
43+
</configuration>
44+
</plugin>
45+
46+
<plugin>
47+
<groupId>org.apache.maven.plugins</groupId>
48+
<artifactId>maven-war-plugin</artifactId>
49+
<version>3.1.0</version>
50+
</plugin>
51+
52+
<plugin>
53+
<groupId>org.codehaus.cargo</groupId>
54+
<artifactId>cargo-maven2-plugin</artifactId>
55+
<version>1.6.4</version>
56+
57+
<executions>
58+
<execution>
59+
<phase>package</phase>
60+
<goals>
61+
<goal>run</goal>
62+
</goals>
63+
</execution>
64+
</executions>
65+
66+
<configuration>
67+
68+
<container>
69+
<containerId>liberty</containerId>
70+
<zipUrlInstaller>
71+
<url>
72+
https://public.dhe.ibm.com/ibmdl/export/pub/software/websphere/wasdev/downloads/wlp/17.0.0.2/wlp-webProfile7-17.0.0.2.zip
73+
</url>
74+
<downloadDir>c:/tmp/download</downloadDir>
75+
<extractDir>c:/tmp/servers</extractDir>
76+
</zipUrlInstaller>
77+
</container>
78+
79+
<deployables>
80+
<deployable>
81+
<groupId>${project.groupId}</groupId>
82+
<artifactId>${project.artifactId}</artifactId>
83+
<type>war</type>
84+
<properties>
85+
<context>/rest-server</context>
86+
</properties>
87+
</deployable>
88+
</deployables>
89+
90+
</configuration>
91+
</plugin>
92+
</plugins>
93+
</build>
94+
95+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.readlearncode;
2+
3+
/**
4+
* Source code github.com/readlearncode
5+
*
6+
* @author Alex Theedom www.readlearncode.com
7+
* @version 1.0
8+
*/
9+
import javax.ws.rs.ApplicationPath;
10+
import javax.ws.rs.core.Application;
11+
12+
@ApplicationPath("/")
13+
public class RESTConfig extends Application {
14+
15+
16+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.readlearncode.restserver;
2+
3+
import javax.ejb.Stateless;
4+
import javax.ws.rs.*;
5+
import javax.ws.rs.core.Context;
6+
import javax.ws.rs.core.HttpHeaders;
7+
import javax.ws.rs.core.MediaType;
8+
import javax.ws.rs.core.Response;
9+
10+
/**
11+
* Source code github.com/readlearncode
12+
*
13+
* @author Alex Theedom www.readlearncode.com
14+
* @version 1.0
15+
*/
16+
@Stateless
17+
@Path("/http-headers")
18+
public class HttpHeaderResource {
19+
20+
// @Context
21+
// private HttpHeaders httpHeaders;
22+
//
23+
// @GET
24+
// @Produces(MediaType.APPLICATION_JSON)
25+
// public Response getAllHttpHeaders(){
26+
// return Response.ok(httpHeaders.getRequestHeaders()).build();
27+
// }
28+
29+
// @GET
30+
// @Produces(MediaType.APPLICATION_JSON)
31+
// public Response getUserAgentHttpHeaders(final @HeaderParam("user-agent") String userAgent){
32+
// return Response.ok(userAgent).build();
33+
// }
34+
35+
@GET
36+
@Produces(MediaType.APPLICATION_JSON)
37+
public Response getAllHttpHeaders(final @Context HttpHeaders httpHeaders){
38+
return Response.ok(httpHeaders.getRequestHeaders()).build();
39+
}
40+
41+
@GET
42+
@Path("/{header-param}")
43+
@Produces(MediaType.APPLICATION_JSON)
44+
public Response getSpecifiedHeader(final @PathParam("header-param") String header_param, final @Context HttpHeaders httpHeaders){
45+
return Response.ok(httpHeaders.getRequestHeader(header_param)).build();
46+
}
47+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.readlearncode.restserver;
2+
3+
import javax.ejb.Stateless;
4+
import javax.ws.rs.GET;
5+
import javax.ws.rs.Path;
6+
import javax.ws.rs.Produces;
7+
import javax.ws.rs.core.Context;
8+
import javax.ws.rs.core.MediaType;
9+
import javax.ws.rs.core.Response;
10+
import javax.ws.rs.core.UriInfo;
11+
12+
/**
13+
* Source code github.com/readlearncode
14+
*
15+
* @author Alex Theedom www.readlearncode.com
16+
* @version 1.0
17+
*/
18+
@Stateless
19+
@Path("/uri-info")
20+
public class UriInfoResource {
21+
22+
@Context
23+
private UriInfo uriInfo;
24+
25+
/* These method paths clash and cannot be used together.*/
26+
@GET
27+
@Path("/{search}")
28+
@Produces(MediaType.APPLICATION_JSON)
29+
public Response getQueryParameters(final @Context UriInfo uriInfo){
30+
return Response.ok(uriInfo.getQueryParameters()).build();
31+
}
32+
33+
// @GET
34+
// @Path("/{path: .*}")
35+
// @Produces(MediaType.APPLICATION_JSON)
36+
// public Response getPathParameters(final @Context UriInfo uriInfo){
37+
// return Response.ok(uriInfo.getPathParameters()).build();
38+
// }
39+
40+
41+
42+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://java.sun.com/xml/ns/javaee"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
5+
</beans>

pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.readlearncode</groupId>
8+
<artifactId>article-code-examples</artifactId>
9+
<version>1.0</version>
10+
<packaging>pom</packaging>
11+
12+
<modules>
13+
<module>jax-rs-context</module>
14+
</modules>
15+
16+
</project>

0 commit comments

Comments
 (0)