mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-14 17:26:22 +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.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import sonia.scm.LastModifiedAware;
|
||||||
import sonia.scm.Manager;
|
import sonia.scm.Manager;
|
||||||
import sonia.scm.ModelObject;
|
import sonia.scm.ModelObject;
|
||||||
|
import sonia.scm.util.Util;
|
||||||
|
|
||||||
//~--- JDK imports ------------------------------------------------------------
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
import javax.ws.rs.Consumes;
|
import javax.ws.rs.Consumes;
|
||||||
import javax.ws.rs.DELETE;
|
import javax.ws.rs.DELETE;
|
||||||
@@ -54,8 +57,11 @@ import javax.ws.rs.Path;
|
|||||||
import javax.ws.rs.PathParam;
|
import javax.ws.rs.PathParam;
|
||||||
import javax.ws.rs.Produces;
|
import javax.ws.rs.Produces;
|
||||||
import javax.ws.rs.WebApplicationException;
|
import javax.ws.rs.WebApplicationException;
|
||||||
|
import javax.ws.rs.core.CacheControl;
|
||||||
import javax.ws.rs.core.Context;
|
import javax.ws.rs.core.Context;
|
||||||
|
import javax.ws.rs.core.EntityTag;
|
||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
|
import javax.ws.rs.core.Request;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
import javax.ws.rs.core.UriInfo;
|
import javax.ws.rs.core.UriInfo;
|
||||||
|
|
||||||
@@ -211,6 +217,8 @@ public abstract class AbstractManagerResource<T extends ModelObject,
|
|||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
|
*
|
||||||
|
* @param request
|
||||||
* @param id
|
* @param id
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
@@ -218,7 +226,7 @@ public abstract class AbstractManagerResource<T extends ModelObject,
|
|||||||
@GET
|
@GET
|
||||||
@Path("{id}")
|
@Path("{id}")
|
||||||
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
|
@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);
|
T item = manager.get(id);
|
||||||
|
|
||||||
@@ -227,20 +235,31 @@ public abstract class AbstractManagerResource<T extends ModelObject,
|
|||||||
throw new WebApplicationException(Response.Status.NOT_FOUND);
|
throw new WebApplicationException(Response.Status.NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
return prepareForReturn(item);
|
prepareForReturn(item);
|
||||||
|
|
||||||
|
return createResponse(request, item);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
|
*
|
||||||
|
* @param request
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@GET
|
@GET
|
||||||
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
|
@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 --------------------------------------------------------------
|
//~--- methods --------------------------------------------------------------
|
||||||
@@ -295,6 +314,85 @@ public abstract class AbstractManagerResource<T extends ModelObject,
|
|||||||
return items;
|
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 ---------------------------------------------------------------
|
//~--- fields ---------------------------------------------------------------
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
|
|||||||
@@ -52,7 +52,10 @@ Sonia.group.Grid = Ext.extend(Sonia.rest.Grid, {
|
|||||||
|
|
||||||
initComponent: function(){
|
initComponent: function(){
|
||||||
var groupStore = new Sonia.rest.JsonStore({
|
var groupStore = new Sonia.rest.JsonStore({
|
||||||
|
proxy: new Ext.data.HttpProxy({
|
||||||
url: restUrl + 'groups.json',
|
url: restUrl + 'groups.json',
|
||||||
|
disableCaching: false
|
||||||
|
}),
|
||||||
fields: [ 'name', 'members', 'description', 'creationDate', 'type'],
|
fields: [ 'name', 'members', 'description', 'creationDate', 'type'],
|
||||||
sortInfo: {
|
sortInfo: {
|
||||||
field: 'name'
|
field: 'name'
|
||||||
|
|||||||
@@ -83,7 +83,10 @@ Sonia.repository.Grid = Ext.extend(Sonia.rest.Grid, {
|
|||||||
initComponent: function(){
|
initComponent: function(){
|
||||||
|
|
||||||
var repositoryStore = new Sonia.rest.JsonStore({
|
var repositoryStore = new Sonia.rest.JsonStore({
|
||||||
|
proxy: new Ext.data.HttpProxy({
|
||||||
url: restUrl + 'repositories.json',
|
url: restUrl + 'repositories.json',
|
||||||
|
disableCaching: false
|
||||||
|
}),
|
||||||
fields: [ 'id', 'name', 'type', 'contact', 'description', 'creationDate', 'url', 'public', 'permissions' ],
|
fields: [ 'id', 'name', 'type', 'contact', 'description', 'creationDate', 'url', 'public', 'permissions' ],
|
||||||
sortInfo: {
|
sortInfo: {
|
||||||
field: 'name'
|
field: 'name'
|
||||||
|
|||||||
@@ -58,7 +58,10 @@ Sonia.user.Grid = Ext.extend(Sonia.rest.Grid, {
|
|||||||
initComponent: function(){
|
initComponent: function(){
|
||||||
|
|
||||||
var userStore = new Sonia.rest.JsonStore({
|
var userStore = new Sonia.rest.JsonStore({
|
||||||
|
proxy: new Ext.data.HttpProxy({
|
||||||
url: restUrl + 'users.json',
|
url: restUrl + 'users.json',
|
||||||
|
disableCaching: false
|
||||||
|
}),
|
||||||
fields: [ 'name', 'displayName', 'mail', 'admin', 'creationDate', 'lastModified', 'type'],
|
fields: [ 'name', 'displayName', 'mail', 'admin', 'creationDate', 'lastModified', 'type'],
|
||||||
sortInfo: {
|
sortInfo: {
|
||||||
field: 'name'
|
field: 'name'
|
||||||
|
|||||||
Reference in New Issue
Block a user