Adds automatic generation of 500 Server Error responses in case of an exception during request processing. Renames requestReceived() to processRequest() for uniformity with response processing.

This commit is contained in:
emcho 2010-05-08 21:33:57 +00:00
parent 8a17ce9e25
commit acb573dbab
8 changed files with 47 additions and 25 deletions

View File

@ -71,8 +71,11 @@ public class ConnectivityCheckServer
stunStack.getCredentialsManager().registerAuthority(this); stunStack.getCredentialsManager().registerAuthority(this);
} }
public void requestReceived(StunMessageEvent evt) public void processRequest(StunMessageEvent evt)
{ {
if(true)
throw new NullPointerException("i am the king fo the w");
Request request = (Request)evt.getMessage(); Request request = (Request)evt.getMessage();
//ignore incoming requests that are not meant for the local user. //ignore incoming requests that are not meant for the local user.

View File

@ -142,7 +142,7 @@ public class EventDispatcher
for (RequestListener target : listenersCopy) for (RequestListener target : listenersCopy)
{ {
target.requestReceived(evt); target.processRequest(evt);
} }
synchronized(requestListenersChildren) synchronized(requestListenersChildren)

View File

@ -21,5 +21,5 @@ public interface RequestListener
* *
* @param evt the event containing the incoming STUN request. * @param evt the event containing the incoming STUN request.
*/ */
public void requestReceived(StunMessageEvent evt); public void processRequest(StunMessageEvent evt);
} }

View File

@ -528,7 +528,7 @@ public class StunStack
//validate attributes that need validation. //validate attributes that need validation.
try try
{ {
validateRequestAttributes((Request)msg, event.getRawMessage()); validateRequestAttributes(event);
} }
catch(Exception exc) catch(Exception exc)
{ {
@ -536,8 +536,28 @@ public class StunStack
return; return;
} }
eventDispatcher.fireMessageEvent(event); try
{
eventDispatcher.fireMessageEvent(event);
}
catch (Throwable t)
{
Response error = MessageFactory.createBindingErrorResponse(
ErrorCodeAttribute.SERVER_ERROR,
"Oops! Something went wrong on our side :(");
try
{
sendResponse(event.getTransactionID().getTransactionID(),
error, event.getLocalAddress(),
event.getRemoteAddress());
}
catch(Exception exc)
{
logger.log(Level.FINE, "Couldn't send a server error "
+ "response", exc);
}
}
} }
//response //response
else if(msg instanceof Response) else if(msg instanceof Response)
@ -602,9 +622,8 @@ public class StunStack
* Executes actions related specific attributes like asserting proper * Executes actions related specific attributes like asserting proper
* checksums or verifying the validity of user names. * checksums or verifying the validity of user names.
* *
* @param request the {@link Request} to validate. * @param evt the {@link StunMessageEvent} that contains the {@link
* @param rawMessage the {@link RawMessage} containing the bytes of the * Request} that we need to validate.
* {@link Request} so that we could check MESSAGE-INTEGRITY if necessary..
* *
* @throws IllegalArgumentException if there's something in the * @throws IllegalArgumentException if there's something in the
* <tt>attribute</tt> that caused us to discard the whole message (e.g. an * <tt>attribute</tt> that caused us to discard the whole message (e.g. an
@ -613,10 +632,10 @@ public class StunStack
* @throws StunException if we fail while sending an error response. * @throws StunException if we fail while sending an error response.
* @throws IOException if we fail while sending an error response. * @throws IOException if we fail while sending an error response.
*/ */
private void validateRequestAttributes(Request request, private void validateRequestAttributes(StunMessageEvent evt)
RawMessage rawMessage)
throws IllegalArgumentException, StunException, IOException throws IllegalArgumentException, StunException, IOException
{ {
Message request = evt.getMessage();
//assert valid username //assert valid username
UsernameAttribute unameAttr = (UsernameAttribute)request UsernameAttribute unameAttr = (UsernameAttribute)request
@ -633,8 +652,8 @@ public class StunStack
"unknown user " + username); "unknown user " + username);
sendResponse(request.getTransactionID(), error, sendResponse(request.getTransactionID(), error,
rawMessage.getLocalAddress(), evt.getLocalAddress(),
rawMessage.getRemoteAddress()); evt.getRemoteAddress());
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Non-recognized username: " "Non-recognized username: "
@ -656,8 +675,8 @@ public class StunStack
"missing username"); "missing username");
sendResponse(request.getTransactionID(), error, sendResponse(request.getTransactionID(), error,
rawMessage.getLocalAddress(), evt.getLocalAddress(),
rawMessage.getRemoteAddress()); evt.getRemoteAddress());
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Missing USERNAME in the presence of MESSAGE-INTEGRITY: "); "Missing USERNAME in the presence of MESSAGE-INTEGRITY: ");
@ -665,15 +684,15 @@ public class StunStack
if (!validateMessageIntegrity(msgIntAttr, if (!validateMessageIntegrity(msgIntAttr,
new String(unameAttr.getUsername()), new String(unameAttr.getUsername()),
rawMessage.getBytes(), 0)) evt.getRawMessage().getBytes(), 0))
{ {
Response error = MessageFactory.createBindingErrorResponse( Response error = MessageFactory.createBindingErrorResponse(
ErrorCodeAttribute.UNAUTHORIZED, ErrorCodeAttribute.UNAUTHORIZED,
"Wrong MESSAGE-INTEGRITY value"); "Wrong MESSAGE-INTEGRITY value");
sendResponse(request.getTransactionID(), error, sendResponse(request.getTransactionID(), error,
rawMessage.getLocalAddress(), evt.getLocalAddress(),
rawMessage.getRemoteAddress()); evt.getRemoteAddress());
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Wrong MESSAGE-INTEGRITY value."); "Wrong MESSAGE-INTEGRITY value.");
@ -687,8 +706,8 @@ public class StunStack
"Missing MESSAGE-INTEGRITY."); "Missing MESSAGE-INTEGRITY.");
sendResponse(request.getTransactionID(), error, sendResponse(request.getTransactionID(), error,
rawMessage.getLocalAddress(), evt.getLocalAddress(),
rawMessage.getRemoteAddress()); evt.getRemoteAddress());
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Missing MESSAGE-INTEGRITY."); "Missing MESSAGE-INTEGRITY.");
} }
@ -711,8 +730,8 @@ public class StunStack
"missing username", sBuff.toString().toCharArray()); "missing username", sBuff.toString().toCharArray());
sendResponse(request.getTransactionID(), error, sendResponse(request.getTransactionID(), error,
rawMessage.getLocalAddress(), evt.getLocalAddress(),
rawMessage.getRemoteAddress()); evt.getRemoteAddress());
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Missing MESSAGE-INTEGRITY."); "Missing MESSAGE-INTEGRITY.");

View File

@ -255,7 +255,7 @@ public class MessageEventDispatchingTest extends TestCase
* *
* @param evt the event containing the incoming request. * @param evt the event containing the incoming request.
*/ */
public void requestReceived(StunMessageEvent evt) public void processRequest(StunMessageEvent evt)
{ {
synchronized (this) synchronized (this)
{ {

View File

@ -468,7 +468,7 @@ public class TransactionSupportTests extends TestCase
* *
* @param evt the {@link StunMessageEvent} to log. * @param evt the {@link StunMessageEvent} to log.
*/ */
public void requestReceived(StunMessageEvent evt) public void processRequest(StunMessageEvent evt)
{ {
synchronized(this) synchronized(this)

View File

@ -381,7 +381,7 @@ public class ShallowStackTest extends TestCase {
* @param evt the <tt>StunMessageEvent</tt> containing the details of * @param evt the <tt>StunMessageEvent</tt> containing the details of
* the newly received request. * the newly received request.
*/ */
public void requestReceived(StunMessageEvent evt) public void processRequest(StunMessageEvent evt)
{ {
synchronized(this) synchronized(this)
{ {

View File

@ -95,7 +95,7 @@ public class ResponseSequenceServer
* different from a Response on the current position. * different from a Response on the current position.
* @param evt the event being dispatched * @param evt the event being dispatched
*/ */
public void requestReceived(StunMessageEvent evt) public void processRequest(StunMessageEvent evt)
{ {
if(messageSequence.isEmpty()) if(messageSequence.isEmpty())
return; return;