Skip to content

Commit

Permalink
node api backend cache
Browse files Browse the repository at this point in the history
  • Loading branch information
rymsha committed Jan 26, 2024
1 parent b445f19 commit b8daac3
Showing 1 changed file with 47 additions and 20 deletions.
67 changes: 47 additions & 20 deletions src/main/java/com/enonic/xp/booster/BoosterApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ private static class CacheItem

final ByteArrayOutputStream gzipData;

final Map<String, String> headers;
final Map<String, Object> headers;

final int contentLength;

CacheItem( final String url, final String contentType, final Map<String, String> headers, final ByteArrayOutputStream data )
CacheItem( final String url, final String contentType, final Map<String, Object> headers, final ByteArrayOutputStream data )
{
this.url = url;
this.contentType = contentType;
Expand All @@ -116,7 +116,7 @@ private static class CacheItem
}
}

public CacheItem( final String url, final String contentType, final Map<String, String> headers, final int contentLength,
public CacheItem( final String url, final String contentType, final Map<String, Object> headers, final int contentLength,
final ByteArrayOutputStream gzipData)
{
this.url = url;
Expand Down Expand Up @@ -146,10 +146,10 @@ protected void doHandle( final HttpServletRequest req, final HttpServletResponse
final String fullUrl = getFullURL( req );

final String sha256 = sha256( fullUrl );
final CacheItem cached = cache.get( sha256 );
//final CacheItem cached = cache.get( sha256 );

final NodeId nodeId = NodeId.from( sha256 );
final CacheItem cacheItem = runWithAdminRole( () -> {
final CacheItem cached = runWithAdminRole( () -> {

try
{
Expand All @@ -163,13 +163,13 @@ protected void doHandle( final HttpServletRequest req, final HttpServletResponse
final int contentLength = node.data().getLong( "contentLength" ).intValue();
final String url = node.data().getString( "url" );
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final ByteSource body = nodeService.getBinary( nodeId, BinaryReference.from( "gzipData" ) );
final ByteSource body = nodeService.getBinary( nodeId, BinaryReference.from( "data.gzip" ) );
if ( body == null )
{
return null;
}
body.copyTo( outputStream );
return new CacheItem( url, contentType, (Map<String, String>) (Map<String, ?>) headers, contentLength, outputStream );
return new CacheItem( url, contentType, headers, contentLength, outputStream );
}
catch ( NodeNotFoundException e )
{
Expand Down Expand Up @@ -267,13 +267,14 @@ protected void doHandle( final HttpServletRequest req, final HttpServletResponse
return;
}

final CacheItem cachedItem =
new CacheItem( sha256, servletResponse.getContentType(), servletResponse.headers, servletResponse.baos );
/*final CacheItem cachedItem =
new CacheItem( sha256, servletResponse.getContentType(), servletResponse.headers, servletResponse.baos );*/
runWithAdminRole( () -> {
final PropertyTree data = new PropertyTree();
data.setString( "url", fullUrl );
data.setString( "contentType", servletResponse.getContentType() );
data.setLong( "contentLength", (long) servletResponse.baos.size() );
data.setBinaryReference( "gzipData", BinaryReference.from( "data.gzip" ) );

final PropertySet headersPropertyTree = data.newSet();

Expand All @@ -283,23 +284,47 @@ protected void doHandle( final HttpServletRequest req, final HttpServletResponse
if (nodeService.nodeExists( nodeId)) {
return null;
} else {
nodeService.create( CreateNodeParams.create()
.parent( NodePath.ROOT )
.setNodeId( nodeId )
.data( data )
.attachBinary( BinaryReference.from( "gzipData" ),
ByteSource.wrap( servletResponse.baos.toByteArray() ) )
.name( sha256 )
.build() );
return null;
ByteArrayOutputStream gzipData = new ByteArrayOutputStream();
try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream( gzipData ))
{
servletResponse.baos.writeTo( gzipOutputStream );
}
final Node node = nodeService.create( CreateNodeParams.create()
.parent( NodePath.ROOT )
.setNodeId( nodeId )
.data( data )
.attachBinary( BinaryReference.from( "data.gzip" ),
ByteSource.wrap( gzipData.toByteArray() ) )
.name( sha256 )
.build() );
return null;
}
} );
cache.put( fullUrl, cachedItem );
//cache.put( sha256, cachedItem );
}

private static void copyHeaders( final HttpServletResponse res, final CacheItem cached )
{
cached.headers.forEach( res::setHeader );
for ( var o : cached.headers.entrySet() )
{
if (o.getValue() instanceof String) {
res.setHeader( o.getKey(), (String) o.getValue() );
} else if (o.getValue() instanceof Collection) {
int i = 0;
for ( String s : (Collection<String>) o.getValue() )
{
if ( i == 0 )
{
res.setHeader( o.getKey(), s );
}
else
{
res.addHeader( o.getKey(), s );
}
i++;
}
}
}
}

private static boolean supportsGzip( final HttpServletRequest req )
Expand Down Expand Up @@ -444,6 +469,8 @@ private <T> T runWithAdminRole( final Callable<T> callable )
build();
return ContextBuilder.from( context ).
authInfo( authenticationInfo ).
repositoryId( RepositoryId.from( "booster" ) ).
branch( Branch.from( "master" ) ).
build().
callWith( callable );
}
Expand Down

0 comments on commit b8daac3

Please sign in to comment.