mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-14 09:25:43 +01:00
use conditional request to improve performance
This commit is contained in:
@@ -38,12 +38,15 @@ package sonia.scm.api.rest.resources;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import sonia.scm.LastModifiedAware;
|
||||
import sonia.scm.Manager;
|
||||
import sonia.scm.ModelObject;
|
||||
import sonia.scm.util.Util;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.DELETE;
|
||||
@@ -54,8 +57,11 @@ import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
import javax.ws.rs.core.CacheControl;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.EntityTag;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Request;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
|
||||
@@ -211,6 +217,8 @@ public abstract class AbstractManagerResource<T extends ModelObject,
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param request
|
||||
* @param id
|
||||
*
|
||||
* @return
|
||||
@@ -218,7 +226,7 @@ public abstract class AbstractManagerResource<T extends ModelObject,
|
||||
@GET
|
||||
@Path("{id}")
|
||||
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
|
||||
public T get(@PathParam("id") String id)
|
||||
public Response get(@Context Request request, @PathParam("id") String id)
|
||||
{
|
||||
T item = manager.get(id);
|
||||
|
||||
@@ -227,20 +235,31 @@ public abstract class AbstractManagerResource<T extends ModelObject,
|
||||
throw new WebApplicationException(Response.Status.NOT_FOUND);
|
||||
}
|
||||
|
||||
return prepareForReturn(item);
|
||||
prepareForReturn(item);
|
||||
|
||||
return createResponse(request, item);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@GET
|
||||
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
|
||||
public Collection<T> getAll()
|
||||
public Response getAll(@Context Request request)
|
||||
{
|
||||
return prepareForReturn(manager.getAll());
|
||||
Collection<T> items = manager.getAll();
|
||||
|
||||
if (Util.isNotEmpty(items))
|
||||
{
|
||||
prepareForReturn(manager.getAll());
|
||||
}
|
||||
|
||||
return createResponse(request, items);
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
@@ -295,6 +314,85 @@ public abstract class AbstractManagerResource<T extends ModelObject,
|
||||
return items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param rb
|
||||
*/
|
||||
private void addCacheControl(Response.ResponseBuilder rb)
|
||||
{
|
||||
CacheControl cc = new CacheControl();
|
||||
|
||||
rb.cacheControl(cc);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param request
|
||||
* @param item
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private Response createResponse(Request request, T item)
|
||||
{
|
||||
EntityTag e = new EntityTag(Integer.toString(item.hashCode()));
|
||||
Date lastModified = getLastModified(item);
|
||||
Response.ResponseBuilder builder =
|
||||
request.evaluatePreconditions(lastModified, e);
|
||||
|
||||
if (builder == null)
|
||||
{
|
||||
builder = Response.ok(item).tag(e).lastModified(lastModified);
|
||||
}
|
||||
|
||||
addCacheControl(builder);
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param request
|
||||
* @param items
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private Response createResponse(Request request, Collection<T> items)
|
||||
{
|
||||
Date lastModified = getLastModified(manager);
|
||||
Response.ResponseBuilder builder =
|
||||
request.evaluatePreconditions(lastModified);
|
||||
|
||||
if (builder == null)
|
||||
{
|
||||
builder = Response.ok(items).lastModified(lastModified);
|
||||
}
|
||||
|
||||
addCacheControl(builder);
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param item
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private Date getLastModified(LastModifiedAware item)
|
||||
{
|
||||
return new Date(item.getLastModified());
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
|
||||
@@ -52,7 +52,10 @@ Sonia.group.Grid = Ext.extend(Sonia.rest.Grid, {
|
||||
|
||||
initComponent: function(){
|
||||
var groupStore = new Sonia.rest.JsonStore({
|
||||
proxy: new Ext.data.HttpProxy({
|
||||
url: restUrl + 'groups.json',
|
||||
disableCaching: false
|
||||
}),
|
||||
fields: [ 'name', 'members', 'description', 'creationDate', 'type'],
|
||||
sortInfo: {
|
||||
field: 'name'
|
||||
|
||||
@@ -83,7 +83,10 @@ Sonia.repository.Grid = Ext.extend(Sonia.rest.Grid, {
|
||||
initComponent: function(){
|
||||
|
||||
var repositoryStore = new Sonia.rest.JsonStore({
|
||||
proxy: new Ext.data.HttpProxy({
|
||||
url: restUrl + 'repositories.json',
|
||||
disableCaching: false
|
||||
}),
|
||||
fields: [ 'id', 'name', 'type', 'contact', 'description', 'creationDate', 'url', 'public', 'permissions' ],
|
||||
sortInfo: {
|
||||
field: 'name'
|
||||
|
||||
@@ -58,7 +58,10 @@ Sonia.user.Grid = Ext.extend(Sonia.rest.Grid, {
|
||||
initComponent: function(){
|
||||
|
||||
var userStore = new Sonia.rest.JsonStore({
|
||||
proxy: new Ext.data.HttpProxy({
|
||||
url: restUrl + 'users.json',
|
||||
disableCaching: false
|
||||
}),
|
||||
fields: [ 'name', 'displayName', 'mail', 'admin', 'creationDate', 'lastModified', 'type'],
|
||||
sortInfo: {
|
||||
field: 'name'
|
||||
|
||||
Reference in New Issue
Block a user