Skip to content

Commit 752da22

Browse files
committed
Add ServletContext parameter to RpcLogger methods, allow only one initialization of RpcLoggerProvider
Various other improvements as suggested by niloc32
1 parent 7593341 commit 752da22

20 files changed

Lines changed: 675 additions & 438 deletions

user/build.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@
163163
</tokenfilter>
164164
</filterchain>
165165
</move>
166-
<move file="${project.build}/no-servlet-src/META-INF/services/com.google.gwt.user.server.rpc.logging.LoggerProvider" tofile="${project.build}/jakarta-src/META-INF/services/com.google.gwt.user.server.rpc.logging.jakarta.LoggerProvider">
166+
<move file="${project.build}/no-servlet-src/META-INF/services/com.google.gwt.user.server.rpc.logging.RpcLoggerProvider" tofile="${project.build}/jakarta-src/META-INF/services/com.google.gwt.user.server.rpc.logging.jakarta.RpcLoggerProvider">
167167
<filterchain>
168168
<tokenfilter>
169169
<replacestring from="com.google.gwt.user.server.rpc.logging" to="com.google.gwt.user.server.rpc.logging.jakarta"/>

user/src/META-INF/services/com.google.gwt.user.server.rpc.logging.LoggerProvider renamed to user/src/META-INF/services/com.google.gwt.user.server.rpc.logging.RpcLoggerProvider

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
com.google.gwt.user.server.rpc.logging.JulLoggerProvider
22
com.google.gwt.user.server.rpc.logging.Log4jProvider
33
com.google.gwt.user.server.rpc.logging.PlatformLoggerProvider
4-
# No no-args constructor: com.google.gwt.user.server.rpc.logging.ServletContextLoggerProvider
4+
com.google.gwt.user.server.rpc.logging.ServletContextLoggerProvider

user/src/com/google/gwt/user/server/rpc/AbstractRemoteServiceServlet.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.io.IOException;
2121

22+
import javax.servlet.ServletContext;
2223
import javax.servlet.ServletException;
2324
import javax.servlet.http.HttpServlet;
2425
import javax.servlet.http.HttpServletRequest;
@@ -104,7 +105,9 @@ protected void doUnexpectedFailure(Throwable e) {
104105
*/
105106
throw new RuntimeException("Unable to report failure", e);
106107
}
107-
RPCServletUtils.writeResponseForUnexpectedFailure(getThreadLocalResponse(), e);
108+
ServletContext servletContext = getServletContext();
109+
RPCServletUtils.writeResponseForUnexpectedFailure(servletContext,
110+
getThreadLocalResponse(), e);
108111
}
109112

110113
/**

user/src/com/google/gwt/user/server/rpc/RPCServletUtils.java

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
*/
1616
package com.google.gwt.user.server.rpc;
1717

18-
19-
import com.google.gwt.user.server.rpc.logging.LogManager;
20-
import com.google.gwt.user.server.rpc.logging.Logger;
18+
import com.google.gwt.user.server.rpc.logging.RpcLogManager;
19+
import com.google.gwt.user.server.rpc.logging.RpcLogger;
2120

2221
import java.io.ByteArrayOutputStream;
2322
import java.io.IOException;
@@ -40,7 +39,7 @@
4039
*/
4140
public class RPCServletUtils {
4241

43-
private static final Logger logger = LogManager.getLogger(RPCServletUtils.class);
42+
private static final RpcLogger logger = RpcLogManager.getLogger(RPCServletUtils.class);
4443

4544
public static final String CHARSET_UTF8_NAME = "UTF-8";
4645

@@ -67,9 +66,11 @@ public class RPCServletUtils {
6766

6867
private static final String CONTENT_ENCODING_GZIP = "gzip";
6968

70-
private static final String CONTENT_TYPE_APPLICATION_JSON_UTF8 = "application/json; charset=utf-8";
69+
private static final String CONTENT_TYPE_APPLICATION_JSON_UTF8 =
70+
"application/json; charset=utf-8";
7171

72-
private static final String GENERIC_FAILURE_MSG = "The call failed on the server; see server log for details";
72+
private static final String GENERIC_FAILURE_MSG =
73+
"The call failed on the server; see server log for details";
7374

7475
private static final String GWT_RPC_CONTENT_TYPE = "text/x-gwt-rpc";
7576

@@ -324,14 +325,6 @@ public static boolean shouldGzipResponseContent(HttpServletRequest request,
324325
&& exceedsUncompressedContentLengthLimit(responseContent);
325326
}
326327

327-
@Deprecated
328-
public static void writeResponse(ServletContext servletContext,
329-
HttpServletResponse response, String responseContent, boolean gzipResponse)
330-
throws IOException {
331-
writeResponse(response, responseContent, gzipResponse);
332-
}
333-
334-
335328
/**
336329
* Write the response content into the {@link HttpServletResponse}. If
337330
* <code>gzipResponse</code> is <code>true</code>, the response content will
@@ -344,7 +337,7 @@ public static void writeResponse(ServletContext servletContext,
344337
* @throws IOException if reading, writing, or closing the response's output
345338
* stream fails
346339
*/
347-
public static void writeResponse(
340+
public static void writeResponse(ServletContext servletContext,
348341
HttpServletResponse response, String responseContent, boolean gzipResponse)
349342
throws IOException {
350343

@@ -375,7 +368,7 @@ public static void writeResponse(
375368
}
376369

377370
if (caught != null) {
378-
logger.error("Unable to compress response", caught);
371+
logger.error("Unable to compress response", caught, servletContext);
379372
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
380373
return;
381374
}
@@ -390,13 +383,6 @@ public static void writeResponse(
390383
response.getOutputStream().write(responseBytes);
391384
}
392385

393-
@Deprecated
394-
public static void writeResponseForUnexpectedFailure(
395-
ServletContext servletContext, HttpServletResponse response,
396-
Throwable failure) {
397-
writeResponseForUnexpectedFailure(response, failure);
398-
}
399-
400386
/**
401387
* Called when the servlet itself has a problem, rather than the invoked
402388
* third-party method. It writes a simple 500 message back to the client.
@@ -405,8 +391,9 @@ public static void writeResponseForUnexpectedFailure(
405391
* @param failure
406392
*/
407393
public static void writeResponseForUnexpectedFailure(
408-
HttpServletResponse response, Throwable failure) {
409-
logger.error("Exception while dispatching incoming RPC call", failure);
394+
ServletContext servletContext, HttpServletResponse response,
395+
Throwable failure) {
396+
logger.error("Exception while dispatching incoming RPC call", failure, servletContext);
410397

411398
// Send GENERIC_FAILURE_MSG with 500 status.
412399
//
@@ -422,7 +409,7 @@ public static void writeResponseForUnexpectedFailure(
422409
} catch (IOException ex) {
423410
logger.error(
424411
"respondWithUnexpectedFailure failed while sending the previous failure to the client",
425-
ex);
412+
ex, servletContext);
426413
}
427414
}
428415

@@ -448,7 +435,8 @@ private static void checkCharacterEncodingIgnoreCase(
448435
* properly parsed character encoding string if we decide to make this
449436
* change.
450437
*/
451-
if (characterEncoding.toLowerCase(Locale.ROOT).contains(expectedCharSet.toLowerCase(Locale.ROOT))) {
438+
if (characterEncoding.toLowerCase(Locale.ROOT)
439+
.contains(expectedCharSet.toLowerCase(Locale.ROOT))) {
452440
encodingOkay = true;
453441
}
454442
}

user/src/com/google/gwt/user/server/rpc/RemoteServiceServlet.java

Lines changed: 41 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
import com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException;
2323
import com.google.gwt.user.client.rpc.RpcTokenException;
2424
import com.google.gwt.user.client.rpc.SerializationException;
25-
import com.google.gwt.user.server.rpc.logging.LogManager;
26-
import com.google.gwt.user.server.rpc.logging.Logger;
25+
import com.google.gwt.user.server.rpc.logging.RpcLogManager;
26+
import com.google.gwt.user.server.rpc.logging.RpcLogger;
2727

2828
import java.io.IOException;
2929
import java.io.InputStream;
@@ -47,7 +47,7 @@
4747
public class RemoteServiceServlet extends AbstractRemoteServiceServlet
4848
implements SerializationPolicyProvider {
4949

50-
private static final Logger logger = LogManager.getLogger(RemoteServiceServlet.class);
50+
private static final RpcLogger logger = RpcLogManager.getLogger(RemoteServiceServlet.class);
5151

5252
/**
5353
* Loads a serialization policy stored as a servlet resource in the same
@@ -66,7 +66,7 @@ static SerializationPolicy loadSerializationPolicy(HttpServlet servlet,
6666
modulePath = new URL(moduleBaseURL).getPath();
6767
} catch (MalformedURLException ex) {
6868
// log the information, we will default
69-
logger.error("Malformed moduleBaseURL: " + moduleBaseURL, ex);
69+
logger.error("Malformed moduleBaseURL: " + moduleBaseURL, ex, servlet.getServletContext());
7070
}
7171
}
7272

@@ -82,15 +82,16 @@ static SerializationPolicy loadSerializationPolicy(HttpServlet servlet,
8282
+ modulePath
8383
+ ", is not in the same web application as this servlet, "
8484
+ contextPath
85-
+ ". Your module may not be properly configured or your client and server code maybe out of date.";
86-
logger.error(message);
85+
+ ". Your module may not be properly configured " +
86+
"or your client and server code maybe out of date.";
87+
logger.error(message, servlet.getServletContext());
8788
} else {
8889
// Strip off the context path from the module base URL. It should be a
8990
// strict prefix.
9091
String contextRelativePath = modulePath.substring(contextPath.length());
9192

92-
String serializationPolicyFilePath = SerializationPolicyLoader.getSerializationPolicyFileName(contextRelativePath
93-
+ strongName);
93+
String serializationPolicyFilePath = SerializationPolicyLoader.getSerializationPolicyFileName(
94+
contextRelativePath + strongName);
9495

9596
// Open the RPC resource file and read its contents.
9697
InputStream is = servlet.getServletContext().getResourceAsStream(
@@ -102,34 +103,32 @@ static SerializationPolicy loadSerializationPolicy(HttpServlet servlet,
102103
null);
103104
if (serializationPolicy.hasClientFields()) {
104105
if (ENABLE_ENHANCED_CLASSES) {
105-
logger.warn(
106-
"Service deserializes enhanced JPA/JDO classes, which is " +
107-
"unsafe. See https://github.com/gwtproject/gwt/issues/9709 for more " +
108-
"detail on the vulnerability that this presents.");
106+
logger.warn("Service deserializes enhanced JPA/JDO classes, which is " +
107+
"unsafe. See https://github.com/gwtproject/gwt/issues/9709 for more " +
108+
"detail on the vulnerability that this presents.",
109+
servlet.getServletContext());
109110
} else {
110-
logger.error(
111-
"Service deserializes enhanced JPA/JDO classes, which is " +
112-
"unsafe. Review build logs to see which classes are affected, or set " +
113-
ENABLE_GWT_ENHANCED_CLASSES_PROPERTY + " to true to allow using this " +
114-
"service. See https://github.com/gwtproject/gwt/issues/9709 for more " +
115-
"detail.");
111+
logger.error("Service deserializes enhanced JPA/JDO classes, which is " +
112+
"unsafe. Review build logs to see which classes are affected, or set " +
113+
ENABLE_GWT_ENHANCED_CLASSES_PROPERTY + " to true to allow using this " +
114+
"service. See https://github.com/gwtproject/gwt/issues/9709 for more " +
115+
"detail.",
116+
servlet.getServletContext());
116117
serializationPolicy = null;
117118
}
118119
}
119120
} catch (ParseException e) {
120-
logger.error(
121-
"Failed to parse the policy file '"
122-
+ serializationPolicyFilePath + "'", e);
121+
logger.error("Failed to parse the policy file '"
122+
+ serializationPolicyFilePath + "'", e, servlet.getServletContext());
123123
} catch (IOException e) {
124-
logger.error(
125-
"Could not read the policy file '"
126-
+ serializationPolicyFilePath + "'", e);
124+
logger.error("Could not read the policy file '"
125+
+ serializationPolicyFilePath + "'", e, servlet.getServletContext());
127126
}
128127
} else {
129128
String message = "The serialization policy file '"
130129
+ serializationPolicyFilePath
131130
+ "' was not found; did you forget to include it in this deployment?";
132-
logger.error(message);
131+
logger.error(message, servlet.getServletContext());
133132
}
134133
} finally {
135134
if (is != null) {
@@ -152,7 +151,8 @@ static SerializationPolicy loadSerializationPolicy(HttpServlet servlet,
152151
* A cache of moduleBaseURL and serialization policy strong name to
153152
* {@link SerializationPolicy}.
154153
*/
155-
private final Map<String, SerializationPolicy> serializationPolicyCache = new HashMap<String, SerializationPolicy>();
154+
private final Map<String, SerializationPolicy> serializationPolicyCache =
155+
new HashMap<String, SerializationPolicy>();
156156

157157
/**
158158
* The implementation of the service.
@@ -185,27 +185,17 @@ public RemoteServiceServlet(Object delegate) {
185185
}
186186

187187
/**
188-
* Overridden to load the gwt.codeserver.port system property.
188+
* Overridden to load the gwt.codeserver.port system property and initialize the
189+
* {@link RpcLogManager} with a provider name from system properties or the servlet config.
189190
*/
190191
@Override
191192
public void init(ServletConfig config) throws ServletException {
192193
super.init(config);
193-
String providerName = getProviderName(config);
194-
LogManager.initialize(providerName, config.getServletContext());
194+
String providerName = RpcLogManager.getProviderName(config);
195+
RpcLogManager.initialize(providerName);
195196
codeServerPort = getCodeServerPort();
196197
}
197198

198-
private String getProviderName(ServletConfig config) {
199-
String parameterName = "gwt.rpc.logging";
200-
if (System.getProperty(parameterName) != null) {
201-
return System.getProperty(parameterName);
202-
} else if (config.getInitParameter(parameterName) != null) {
203-
return config.getInitParameter(parameterName);
204-
} else {
205-
return config.getServletContext().getInitParameter(parameterName);
206-
}
207-
}
208-
209199
/**
210200
* Returns the value of the gwt.codeserver.port system property, or zero if not defined.
211201
*
@@ -281,12 +271,13 @@ public final SerializationPolicy getSerializationPolicy(String moduleBaseURL,
281271

282272
if (serializationPolicy == null) {
283273
// Failed to get the requested serialization policy; use the default
284-
logger.warn(
285-
"Failed to get the SerializationPolicy '"
274+
logger.warn("Failed to get the SerializationPolicy '"
286275
+ strongName
287276
+ "' for module '"
288277
+ moduleBaseURL
289-
+ "'; a legacy, 1.3.3 compatible, serialization policy will be used. You may experience SerializationExceptions as a result.");
278+
+ "'; a legacy, 1.3.3 compatible, serialization policy will be used. " +
279+
"You may experience SerializationExceptions as a result.",
280+
getServletContext());
290281
serializationPolicy = RPC.getDefaultSerializationPolicy();
291282
}
292283

@@ -334,7 +325,7 @@ public String processCall(String payload) throws SerializationException {
334325
} catch (IncompatibleRemoteServiceException ex) {
335326
logger.error(
336327
"An IncompatibleRemoteServiceException was thrown while processing this call.",
337-
ex);
328+
ex, getServletContext());
338329
return RPC.encodeResponseForFailedRequest(null, ex);
339330
}
340331
return processCall(rpcRequest);
@@ -373,12 +364,12 @@ public String processCall(RPCRequest rpcRequest) throws SerializationException {
373364
} catch (IncompatibleRemoteServiceException ex) {
374365
logger.error(
375366
"An IncompatibleRemoteServiceException was thrown while processing this call.",
376-
ex);
367+
ex,
368+
getServletContext());
377369
return RPC.encodeResponseForFailedRequest(rpcRequest, ex);
378370
} catch (RpcTokenException tokenException) {
379-
logger.error(
380-
"An RpcTokenException was thrown while processing this call.",
381-
tokenException);
371+
logger.error("An RpcTokenException was thrown while processing this call.",
372+
tokenException, getServletContext());
382373
return RPC.encodeResponseForFailedRequest(rpcRequest, tokenException);
383374
}
384375
}
@@ -486,7 +477,7 @@ protected String getCodeServerPolicyUrl(String strongName) {
486477
* no authentication. It should only be used during development.</p>
487478
*/
488479
protected SerializationPolicy loadPolicyFromCodeServer(String url) {
489-
return CODE_SERVER_CLIENT.loadPolicy(url);
480+
return CODE_SERVER_CLIENT.loadPolicy(url, getServletContext());
490481
}
491482

492483
/**
@@ -550,6 +541,6 @@ private void writeResponse(HttpServletRequest request,
550541
boolean gzipEncode = RPCServletUtils.acceptsGzipEncoding(request)
551542
&& shouldCompressResponse(request, response, responsePayload);
552543

553-
RPCServletUtils.writeResponse(response, responsePayload, gzipEncode);
544+
RPCServletUtils.writeResponse(getServletContext(), response, responsePayload, gzipEncode);
554545
}
555546
}

0 commit comments

Comments
 (0)