protected Response process(long wait, String jobId, boolean eatJob)
{
Future<MockHttpResponse> job = jobs.get(jobId);
if (job == null) return Response.status(Response.Status.GONE).build();
MockHttpResponse response = null;
boolean nowait = false;
// I don't want to wait forever!
if (wait <= 0) nowait = true;
if (nowait)
{
if (job.isDone())
{
try
{
response = job.get();
}
catch (Exception e)
{
return Response.serverError().build();
}
}
}
else
{
if (wait > maxWaitMilliSeconds) wait = maxWaitMilliSeconds;
try
{
response = job.get(wait, TimeUnit.MILLISECONDS);
}
catch (InterruptedException e)
{
return Response.serverError().build();
}
catch (ExecutionException e)
{
return Response.serverError().build();
}
catch (TimeoutException e)
{
return Response.status(Response.Status.SERVICE_UNAVAILABLE).build();
}
}
if (response == null)
{
return Response.status(Response.Status.ACCEPTED).build();
}
Response.ResponseBuilder builder = Response.status(response.getStatus());
builder.entity(response.getOutput());
for (String name : response.getOutputHeaders().keySet())
{
List values = response.getOutputHeaders().get(name);
for (Object value : values)
{
builder.header(name, value);
}
}