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

Commit 99f34b1

Browse files
committed
Fix the problem with duplicated FINISHED tracing message in case exception is thrown from MessageBodyWriter on server side.
Log info about mapper used to map exception. Enhanced tracing support integration test by counting tracing events and async resources. Change-Id: If13256856727b985050ac3e5270ce1024f6db4eb
1 parent 72273c7 commit 99f34b1

File tree

9 files changed

+329
-72
lines changed

9 files changed

+329
-72
lines changed

core-server/src/main/java/org/glassfish/jersey/server/ServerRuntime.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,16 @@ private Response mapException(final Throwable originalThrowable) throws Throwabl
579579

580580
if (mappedResponse != null) {
581581
// response successfully mapped
582+
if (LOGGER.isLoggable(Level.FINER)) {
583+
final String message = String.format(
584+
"Exception '%s' has been mapped by '%s' to response '%s' (%s:%s).",
585+
throwable.getLocalizedMessage(),
586+
mapper.getClass().getName(),
587+
mappedResponse.getStatusInfo().getReasonPhrase(),
588+
mappedResponse.getStatusInfo().getStatusCode(),
589+
mappedResponse.getStatusInfo().getFamily());
590+
LOGGER.log(Level.FINER, message);
591+
}
582592
return mappedResponse;
583593
} else {
584594
return Response.noContent().build();
@@ -676,10 +686,10 @@ public OutputStream getOutputStream(final int contentLength) throws IOException
676686
connectionCallbackRunner.onDisconnect(processingContext.asyncContext());
677687
}
678688
throw mpe;
679-
} finally {
680-
tracingLogger.log(ServerTraceEvent.FINISHED, response.getStatusInfo());
681-
tracingLogger.flush(response.getHeaders());
682689
}
690+
tracingLogger.log(ServerTraceEvent.FINISHED, response.getStatusInfo());
691+
tracingLogger.flush(response.getHeaders());
692+
683693
setWrittenResponse(response);
684694

685695
} catch (final Throwable ex) {
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2015 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.integration.tracing;
41+
42+
import javax.ws.rs.GET;
43+
import javax.ws.rs.POST;
44+
import javax.ws.rs.Path;
45+
import javax.ws.rs.PathParam;
46+
import javax.ws.rs.container.AsyncResponse;
47+
import javax.ws.rs.container.Suspended;
48+
49+
/**
50+
* @author Libor Kramolis (libor.kramolis at oracle.com)
51+
*/
52+
@Path("/async")
53+
public class AsyncResource {
54+
55+
@Path("{name}")
56+
@GET
57+
public void get(@PathParam("name") String name, @Suspended final AsyncResponse asyncResponse) {
58+
asyncResponse.resume(new Message(new StringBuffer(name).reverse().toString()));
59+
}
60+
61+
@POST
62+
public void post(Message post, @Suspended final AsyncResponse asyncResponse) {
63+
asyncResponse.resume(new Message(new StringBuffer(post.getText()).reverse().toString()));
64+
}
65+
66+
@Path("sub-resource-method")
67+
@POST
68+
public void postSub(Message post, @Suspended final AsyncResponse asyncResponse) {
69+
asyncResponse.resume(new Message(new StringBuffer(post.getText()).reverse().toString()));
70+
}
71+
72+
@Path("sub-resource-locator")
73+
public AsyncSubResource getSubLoc() {
74+
return new AsyncSubResource();
75+
}
76+
77+
@Path("sub-resource-locator-null")
78+
public AsyncSubResource getSubLocNull() {
79+
return null;
80+
}
81+
82+
@GET
83+
@Path("runtime-exception")
84+
public void getRuntimeException(@Suspended final AsyncResponse asyncResponse) {
85+
asyncResponse.resume(new RuntimeException("Something does not work ..."));
86+
}
87+
88+
@GET
89+
@Path("mapped-exception")
90+
public void getMappedException(@Suspended final AsyncResponse asyncResponse) {
91+
asyncResponse.resume(new TestException("This could be client fault ..."));
92+
}
93+
94+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2015 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.integration.tracing;
41+
42+
import javax.ws.rs.POST;
43+
import javax.ws.rs.Path;
44+
import javax.ws.rs.container.AsyncResponse;
45+
import javax.ws.rs.container.Suspended;
46+
47+
/**
48+
* @author Libor Kramolis (libor.kramolis at oracle.com)
49+
*/
50+
@Path("/")
51+
public class AsyncSubResource {
52+
@POST
53+
public void post(Message post, @Suspended final AsyncResponse asyncResponse) {
54+
asyncResponse.resume(new Message(new StringBuffer(post.getText()).reverse().toString()));
55+
}
56+
57+
@Path("sub-resource-method")
58+
@POST
59+
public void postSub(Message post, @Suspended final AsyncResponse asyncResponse) {
60+
asyncResponse.resume(new Message(new StringBuffer(post.getText()).reverse().toString()));
61+
}
62+
}

tests/integration/tracing-support/src/main/java/org/glassfish/jersey/tests/integration/tracing/MessageBodyReaderTestFormat.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@
6262
@Consumes(Utils.APPLICATION_X_JERSEY_TEST)
6363
public class MessageBodyReaderTestFormat implements MessageBodyReader<Message> {
6464

65+
boolean serverSide = true;
66+
67+
public MessageBodyReaderTestFormat() {
68+
}
69+
70+
public MessageBodyReaderTestFormat(final boolean serverSide) {
71+
this.serverSide = serverSide;
72+
}
73+
6574
@Override
6675
public boolean isReadable(final Class<?> type, final Type genericType, final Annotation[] annotations,
6776
final MediaType mediaType) {
@@ -79,7 +88,15 @@ public Message readFrom(final Class<Message> type, final Type genericType, final
7988
throw new WebApplicationException(
8089
new IllegalArgumentException("Input content '" + line + "' is not in a valid format!"));
8190
}
91+
final String text = line.substring(Utils.FORMAT_PREFIX.length(), line.length() - Utils.FORMAT_SUFFIX.length());
92+
93+
if (serverSide) {
94+
Utils.throwException(text, this,
95+
Utils.TestAction.MESSAGE_BODY_READER_THROW_WEB_APPLICATION,
96+
Utils.TestAction.MESSAGE_BODY_READER_THROW_PROCESSING,
97+
Utils.TestAction.MESSAGE_BODY_READER_THROW_ANY);
98+
}
8299

83-
return new Message(line.substring(Utils.FORMAT_PREFIX.length(), line.length() - Utils.FORMAT_SUFFIX.length()));
100+
return new Message(text);
84101
}
85102
}

tests/integration/tracing-support/src/main/java/org/glassfish/jersey/tests/integration/tracing/MessageBodyWriterTestFormat.java

Lines changed: 18 additions & 1 deletion
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) 2013-2014 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2013-2015 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
@@ -60,6 +60,16 @@
6060
@Provider
6161
@Produces(Utils.APPLICATION_X_JERSEY_TEST)
6262
public class MessageBodyWriterTestFormat implements MessageBodyWriter<Message> {
63+
64+
boolean serverSide = true;
65+
66+
public MessageBodyWriterTestFormat() {
67+
}
68+
69+
public MessageBodyWriterTestFormat(final boolean serverSide) {
70+
this.serverSide = serverSide;
71+
}
72+
6373
@Override
6474
public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations,
6575
final MediaType mediaType) {
@@ -76,6 +86,13 @@ public long getSize(final Message message, final Class<?> type, final Type gener
7686
public void writeTo(final Message message, final Class<?> type, final Type genericType, final Annotation[] annotations,
7787
final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders,
7888
final OutputStream entityStream) throws IOException, WebApplicationException {
89+
if (serverSide) {
90+
Utils.throwException(message.getText(), this,
91+
Utils.TestAction.MESSAGE_BODY_WRITER_THROW_WEB_APPLICATION,
92+
Utils.TestAction.MESSAGE_BODY_WRITER_THROW_PROCESSING,
93+
Utils.TestAction.MESSAGE_BODY_WRITER_THROW_ANY);
94+
}
95+
7996
final OutputStreamWriter writer = new OutputStreamWriter(entityStream, MessageUtils.getCharset(mediaType));
8097
writer.write(Utils.FORMAT_PREFIX);
8198
writer.write(message.getText());

tests/integration/tracing-support/src/main/java/org/glassfish/jersey/tests/integration/tracing/Resource.java

Lines changed: 2 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) 2013 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2013-2015 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
@@ -53,7 +53,6 @@ public class Resource {
5353
@Path("{name}")
5454
@GET
5555
public Message get(@PathParam("name") String name) {
56-
//System.out.println("*** Resource.get: " + name);
5756
return new Message(new StringBuffer(name).reverse().toString());
5857
}
5958

@@ -81,14 +80,13 @@ public SubResource getSubLocNull() {
8180
@GET
8281
@Path("runtime-exception")
8382
public Message getRuntimeException() {
84-
//System.out.println("*** Resource.getRuntimeException");
8583
throw new RuntimeException("Something does not work ...");
8684
}
8785

8886
@GET
8987
@Path("mapped-exception")
9088
public Message getMappedException() {
91-
//System.out.println("*** Resource.getMappedException");
9289
throw new TestException("This could be client fault ...");
9390
}
91+
9492
}

tests/integration/tracing-support/src/main/java/org/glassfish/jersey/tests/integration/tracing/TestExceptionMapper.java

Lines changed: 2 additions & 5 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) 2013 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2013-2015 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
@@ -54,10 +54,7 @@ public class TestExceptionMapper implements ExceptionMapper<TestException> {
5454

5555
@Override
5656
public Response toResponse(TestException throwable) {
57-
System.out.println("*** TestExceptionMapper.toResponse: " + throwable);
58-
Response response = Response.status(501).build();
59-
System.out.println(" * TestExceptionMapper.toResponse: " + response);
60-
return response;
57+
return Response.status(501).build();
6158
}
6259

6360
}

0 commit comments

Comments
 (0)