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);
}
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();
//ignore incoming requests that are not meant for the local user.

View File

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

View File

@ -21,5 +21,5 @@ public interface RequestListener
*
* @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.
try
{
validateRequestAttributes((Request)msg, event.getRawMessage());
validateRequestAttributes(event);
}
catch(Exception exc)
{
@ -536,8 +536,28 @@ public class StunStack
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
else if(msg instanceof Response)
@ -602,9 +622,8 @@ public class StunStack
* Executes actions related specific attributes like asserting proper
* checksums or verifying the validity of user names.
*
* @param request the {@link Request} to validate.
* @param rawMessage the {@link RawMessage} containing the bytes of the
* {@link Request} so that we could check MESSAGE-INTEGRITY if necessary..
* @param evt the {@link StunMessageEvent} that contains the {@link
* Request} that we need to validate.
*
* @throws IllegalArgumentException if there's something in the
* <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 IOException if we fail while sending an error response.
*/
private void validateRequestAttributes(Request request,
RawMessage rawMessage)
private void validateRequestAttributes(StunMessageEvent evt)
throws IllegalArgumentException, StunException, IOException
{
Message request = evt.getMessage();
//assert valid username
UsernameAttribute unameAttr = (UsernameAttribute)request
@ -633,8 +652,8 @@ public class StunStack
"unknown user " + username);
sendResponse(request.getTransactionID(), error,
rawMessage.getLocalAddress(),
rawMessage.getRemoteAddress());
evt.getLocalAddress(),
evt.getRemoteAddress());
throw new IllegalArgumentException(
"Non-recognized username: "
@ -656,8 +675,8 @@ public class StunStack
"missing username");
sendResponse(request.getTransactionID(), error,
rawMessage.getLocalAddress(),
rawMessage.getRemoteAddress());
evt.getLocalAddress(),
evt.getRemoteAddress());
throw new IllegalArgumentException(
"Missing USERNAME in the presence of MESSAGE-INTEGRITY: ");
@ -665,15 +684,15 @@ public class StunStack
if (!validateMessageIntegrity(msgIntAttr,
new String(unameAttr.getUsername()),
rawMessage.getBytes(), 0))
evt.getRawMessage().getBytes(), 0))
{
Response error = MessageFactory.createBindingErrorResponse(
ErrorCodeAttribute.UNAUTHORIZED,
"Wrong MESSAGE-INTEGRITY value");
sendResponse(request.getTransactionID(), error,
rawMessage.getLocalAddress(),
rawMessage.getRemoteAddress());
evt.getLocalAddress(),
evt.getRemoteAddress());
throw new IllegalArgumentException(
"Wrong MESSAGE-INTEGRITY value.");
@ -687,8 +706,8 @@ public class StunStack
"Missing MESSAGE-INTEGRITY.");
sendResponse(request.getTransactionID(), error,
rawMessage.getLocalAddress(),
rawMessage.getRemoteAddress());
evt.getLocalAddress(),
evt.getRemoteAddress());
throw new IllegalArgumentException(
"Missing MESSAGE-INTEGRITY.");
}
@ -711,8 +730,8 @@ public class StunStack
"missing username", sBuff.toString().toCharArray());
sendResponse(request.getTransactionID(), error,
rawMessage.getLocalAddress(),
rawMessage.getRemoteAddress());
evt.getLocalAddress(),
evt.getRemoteAddress());
throw new IllegalArgumentException(
"Missing MESSAGE-INTEGRITY.");

View File

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

View File

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

View File

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

View File

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