Skip to content

Commit fbd7e37

Browse files
committed
Merge branch 'master' of ssh://github.com/arun-gupta/javaee7-samples
2 parents 7d6e312 + 7fcad3f commit fbd7e37

13 files changed

Lines changed: 10071 additions & 19 deletions

File tree

jaxrs/mapping-exceptions/src/test/java/org/javaee7/jaxrs/mapping/exceptions/MyResourceTest.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,50 @@
66

77
package org.javaee7.jaxrs.mapping.exceptions;
88

9+
import static org.junit.Assert.assertEquals;
10+
import static org.junit.Assert.fail;
11+
12+
import java.net.MalformedURLException;
13+
import java.net.URL;
14+
915
import javax.ws.rs.ClientErrorException;
1016
import javax.ws.rs.client.Client;
1117
import javax.ws.rs.client.ClientBuilder;
1218
import javax.ws.rs.client.WebTarget;
19+
20+
import org.jboss.arquillian.container.test.api.Deployment;
21+
import org.jboss.arquillian.junit.Arquillian;
22+
import org.jboss.arquillian.test.api.ArquillianResource;
23+
import org.jboss.shrinkwrap.api.ShrinkWrap;
24+
import org.jboss.shrinkwrap.api.spec.WebArchive;
1325
import org.junit.Before;
1426
import org.junit.Test;
15-
import static org.junit.Assert.*;
27+
import org.junit.runner.RunWith;
1628

1729
/**
1830
*
1931
* @author argupta
2032
*/
33+
@RunWith(Arquillian.class)
2134
public class MyResourceTest {
22-
WebTarget target;
35+
36+
@Deployment(testable = false)
37+
public static WebArchive createDeployment() {
38+
return ShrinkWrap.create(WebArchive.class)
39+
.addClasses(
40+
MyApplication.class, MyResource.class,
41+
OrderNotFoundException.class, OrderNotFoundExceptionMapper.class);
42+
}
43+
@ArquillianResource
44+
private URL base;
45+
46+
private WebTarget target;
2347

2448
@Before
25-
public void setUp() {
49+
public void setUp() throws MalformedURLException {
2650
Client client = ClientBuilder.newClient();
2751
target = client
28-
.target("http://localhost:8080/mapping-exceptions/webresources/order");
52+
.target(new URL(base, "webresources/order").toExternalForm());
2953
}
3054

3155
/**

jaxrs/server-negotiation/src/main/java/org/javaee7/jaxrs/server/negotiation/MyResource.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
*/
4040
package org.javaee7.jaxrs.server.negotiation;
4141

42+
import java.util.List;
43+
4244
import javax.ws.rs.GET;
4345
import javax.ws.rs.Path;
4446
import javax.ws.rs.Produces;
@@ -51,11 +53,11 @@ public class MyResource {
5153
@GET
5254
@Produces({"application/xml; qs=0.75", "application/json; qs=1.0"})
5355
// @Produces({"application/xml", "application/json"})
54-
public Person[] getList() {
55-
Person[] list = new Person[3];
56-
list[0] = new Person("Penny", 1);
57-
list[1] = new Person("Leonard", 2);
58-
list[2] = new Person("Sheldon", 3);
56+
public List<Person> getList() {
57+
People list = new People();
58+
list.add(new Person("Penny", 1));
59+
list.add(new Person("Leonard", 2));
60+
list.add(new Person("Sheldon", 3));
5961

6062
return list;
6163
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.javaee7.jaxrs.server.negotiation;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import javax.xml.bind.annotation.XmlElement;
7+
import javax.xml.bind.annotation.XmlRootElement;
8+
9+
@XmlRootElement
10+
public class People extends ArrayList<Person> {
11+
12+
private static final long serialVersionUID = 1L;
13+
14+
@XmlElement(name = "person")
15+
public List<Person> getPeople() {
16+
return this;
17+
}
18+
}

jaxrs/server-negotiation/src/test/java/org/javaee7/jaxrs/server/negotiation/MyResourceTest.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,48 @@
77
package org.javaee7.jaxrs.server.negotiation;
88

99
import java.io.IOException;
10+
import java.net.MalformedURLException;
11+
import java.net.URL;
12+
1013
import javax.ws.rs.client.Client;
1114
import javax.ws.rs.client.ClientBuilder;
1215
import javax.ws.rs.client.WebTarget;
16+
1317
import org.custommonkey.xmlunit.XMLAssert;
18+
import org.jboss.arquillian.container.test.api.Deployment;
19+
import org.jboss.arquillian.junit.Arquillian;
20+
import org.jboss.arquillian.test.api.ArquillianResource;
21+
import org.jboss.shrinkwrap.api.ShrinkWrap;
22+
import org.jboss.shrinkwrap.api.spec.WebArchive;
1423
import org.json.JSONException;
15-
import org.junit.After;
16-
import org.junit.AfterClass;
1724
import org.junit.Before;
18-
import org.junit.BeforeClass;
1925
import org.junit.Test;
20-
import static org.junit.Assert.*;
26+
import org.junit.runner.RunWith;
2127
import org.skyscreamer.jsonassert.JSONAssert;
2228
import org.skyscreamer.jsonassert.JSONCompareMode;
2329
import org.xml.sax.SAXException;
2430

2531
/**
2632
* @author Arun Gupta
2733
*/
34+
@RunWith(Arquillian.class)
2835
public class MyResourceTest {
2936

30-
WebTarget target;
31-
37+
@Deployment(testable = false)
38+
public static WebArchive createDeployment() {
39+
return ShrinkWrap.create(WebArchive.class)
40+
.addClasses(MyApplication.class, MyResource.class, People.class, Person.class);
41+
}
42+
43+
@ArquillianResource
44+
private URL base;
45+
46+
private WebTarget target;
47+
3248
@Before
33-
public void setUp() {
49+
public void setUp() throws MalformedURLException {
3450
Client client = ClientBuilder.newClient();
35-
target = client
36-
.target("http://localhost:8080/server-negotiation/webresources/persons");
51+
target = client.target(new URL(base, "webresources/persons").toExternalForm());
3752
}
3853

3954
@Test
@@ -55,7 +70,7 @@ public void testJson2() throws JSONException {
5570
@Test
5671
public void testXml() throws JSONException, SAXException, IOException {
5772
String response = target.request().accept("application/xml").get(String.class);
58-
XMLAssert.assertXMLEqual("<collection><person><age>1</age><name>Penny</name></person><person><age>2</age><name>Leonard</name></person><person><age>3</age><name>Sheldon</name></person></collection>",
73+
XMLAssert.assertXMLEqual("<people><person><age>1</age><name>Penny</name></person><person><age>2</age><name>Leonard</name></person><person><age>3</age><name>Sheldon</name></person></people>",
5974
response);
6075
}
6176

websocket/atmosphere-chat/pom.xml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
<parent>
5+
<groupId>org.javaee7.websocket</groupId>
6+
<artifactId>websocket-samples</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
<groupId>org.javaee7.websocket</groupId>
12+
<artifactId>atmosphere-chat</artifactId>
13+
<packaging>war</packaging>
14+
<version>1.0-SNAPSHOT</version>
15+
<name>atmosphere-chat</name>
16+
<url>http://maven.apache.org</url>
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.atmosphere.client</groupId>
20+
<artifactId>javascript</artifactId>
21+
<version>2.0.7</version>
22+
<type>war</type>
23+
</dependency>
24+
<dependency>
25+
<groupId>ch.qos.logback</groupId>
26+
<artifactId>logback-classic</artifactId>
27+
<version>1.0.13</version>
28+
</dependency>
29+
30+
<dependency>
31+
<groupId>ch.qos.logback</groupId>
32+
<artifactId>logback-core</artifactId>
33+
<version>1.0.13</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.atmosphere</groupId>
37+
<artifactId>atmosphere-runtime</artifactId>
38+
<version>2.0.3</version>
39+
</dependency>
40+
<!-- Uncomment to use Redis as a backend for ClouPush
41+
<dependency>
42+
<groupId>org.atmosphere</groupId>
43+
<artifactId>atmosphere-redis</artifactId>
44+
<version>${project.version}</version>
45+
</dependency>
46+
-->
47+
<dependency>
48+
<groupId>org.codehaus.jackson</groupId>
49+
<artifactId>jackson-mapper-asl</artifactId>
50+
<version>1.9.3</version>
51+
</dependency>
52+
</dependencies>
53+
</project>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2013 Jeanfrancois Arcand
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.javaee7.websocket.atmosphere;
17+
18+
import org.atmosphere.config.service.Disconnect;
19+
import org.atmosphere.config.service.ManagedService;
20+
import org.atmosphere.config.service.Ready;
21+
import org.atmosphere.cpr.AtmosphereResource;
22+
import org.atmosphere.cpr.AtmosphereResourceEvent;
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
25+
26+
import java.io.IOException;
27+
28+
/**
29+
* Simple annotated class that demonstrate the power of Atmosphere. This class supports all transports, support
30+
* message length guarantee, heart beat, message cache thanks to the @ManagedAService.
31+
*
32+
* The client will first try with WebSocket and then fallback using the client's preference.
33+
*/
34+
@ManagedService(path = "/chat")
35+
public class ChatEndpoint {
36+
private final Logger logger = LoggerFactory.getLogger(ChatEndpoint.class);
37+
38+
/**
39+
* Invoked when the connection as been fully established and suspended, e.g ready for receiving messages.
40+
*
41+
* @param r
42+
*/
43+
@Ready
44+
public void onReady(final AtmosphereResource r) {
45+
logger.info("Browser {} connected.", r.uuid());
46+
}
47+
48+
/**
49+
* Invoked when the client disconnect or when an unexpected closing of the underlying connection happens.
50+
*
51+
* @param event
52+
*/
53+
@Disconnect
54+
public void onDisconnect(AtmosphereResourceEvent event) {
55+
if (event.isCancelled()) {
56+
logger.info("Browser {} unexpectedly disconnected", event.getResource().uuid());
57+
} else if (event.isClosedByClient()) {
58+
logger.info("Browser {} closed the connection", event.getResource().uuid());
59+
}
60+
}
61+
62+
/**
63+
* Simple annotated class that demonstrate how {@link org.atmosphere.config.managed.Encoder} and {@link org.atmosphere.config.managed.Decoder
64+
* can be used.
65+
*
66+
* @param message an instance of {@link Message}
67+
* @return
68+
* @throws IOException
69+
*/
70+
@org.atmosphere.config.service.Message(encoders = {JacksonEncoder.class}, decoders = {JacksonDecoder.class})
71+
public Message onMessage(Message message) throws IOException {
72+
logger.info("{} just send {}", message.getAuthor(), message.getMessage());
73+
return message;
74+
}
75+
76+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2013 Jeanfrancois Arcand
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.javaee7.websocket.atmosphere;
17+
18+
import org.atmosphere.config.managed.Decoder;
19+
import org.codehaus.jackson.map.ObjectMapper;
20+
21+
import java.io.IOException;
22+
23+
/**
24+
* Decode a String into a {@link Message}.
25+
*/
26+
public class JacksonDecoder implements Decoder<String, Message> {
27+
28+
private final ObjectMapper mapper = new ObjectMapper();
29+
30+
@Override
31+
public Message decode(String s) {
32+
try {
33+
return mapper.readValue(s, Message.class);
34+
} catch (IOException e) {
35+
throw new RuntimeException(e);
36+
}
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2013 Jeanfrancois Arcand
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.javaee7.websocket.atmosphere;
17+
18+
import org.atmosphere.config.managed.Encoder;
19+
import org.codehaus.jackson.map.ObjectMapper;
20+
21+
import java.io.IOException;
22+
23+
/**
24+
* Encode a {@link Message} into a String
25+
*/
26+
public class JacksonEncoder implements Encoder<Message, String> {
27+
28+
private final ObjectMapper mapper = new ObjectMapper();
29+
30+
@Override
31+
public String encode(Message m) {
32+
try {
33+
return mapper.writeValueAsString(m);
34+
} catch (IOException e) {
35+
throw new RuntimeException(e);
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)