Permalink
Browse files

Merge pull request #706 from sonatype/nxcm-4861

NXCM-4861: Fixing the ArtifactStoreHelper.storeItemWithChecksums method
  • Loading branch information...
2 parents 6531b65 + 8a44439 commit 1b147dd47cf05476a2cc27dc7e9c8d40e196f26c @cstamas cstamas committed Jan 4, 2013
@@ -17,6 +17,8 @@
import java.util.Stack;
import org.codehaus.plexus.util.StringUtils;
+import org.sonatype.nexus.proxy.repository.GroupRepository;
+import org.sonatype.nexus.proxy.repository.ProxyRepository;
public class RequestContext
extends HashMap<String, Object>
@@ -30,13 +32,13 @@
/** Context URL of the original resource requested on the incoming connector. */
public static final String CTX_REQUEST_URL = "request.url";
- /** Context flag to mark a request local only. */
+ /** Context flag to mark a request local only. For {@link ProxyRepository} instances: do not attempt remote access at all, else: no effect. */
public static final String CTX_LOCAL_ONLY_FLAG = "request.localOnly";
- /** Context flag to mark a request local only. */
+ /** Context flag to mark a request local only. For {@link ProxyRepository} instances: force remote access -- might still serve local if cache is fresh, else: no effect. */
public static final String CTX_REMOTE_ONLY_FLAG = "request.remoteOnly";
- /** Context flag to mark a request local only. */
+ /** Context flag to mark a request local only. For {@link GroupRepository} instances: do not "dive" into members, else: no effect. */
public static final String CTX_GROUP_LOCAL_ONLY_FLAG = "request.groupLocalOnly";
/** Context key for condition "if-modified-since" */
@@ -55,15 +57,11 @@
public RequestContext()
{
- super();
-
- this.parent = null;
+ setParentContext( null );
}
public RequestContext( RequestContext parent )
{
- this();
-
setParentContext( parent );
}
@@ -180,7 +178,7 @@ public boolean isRequestRemoteOnly()
/**
* Sets the request remote only.
*
- * @param requestremoteOnly the new request remote only
+ * @param requestRemoteOnly the new request remote only
*/
public void setRequestRemoteOnly( boolean requestRemoteOnly )
{
@@ -207,7 +205,7 @@ public boolean isRequestGroupLocalOnly()
/**
* Sets the request group local only.
*
- * @param requestremoteOnly the new request group local only
+ * @param requestGroupLocal the new request group local only
*/
public void setRequestGroupLocalOnly( boolean requestGroupLocal )
{
@@ -347,26 +345,30 @@ public void setRequestAppRootUrl( String url )
*/
public Map<String, Object> flatten()
{
- HashMap<String, Object> result = new HashMap<String, Object>();
-
+ final HashMap<String, Object> result = new HashMap<String, Object>();
RequestContext ctx = this;
-
- Stack<RequestContext> stack = new Stack<RequestContext>();
-
+ final Stack<RequestContext> stack = new Stack<RequestContext>();
while ( ctx != null )
{
stack.push( ctx );
-
ctx = ctx.getParentContext();
}
-
while ( !stack.isEmpty() )
{
ctx = stack.pop();
-
result.putAll( ctx );
}
-
return result;
}
+
+ // ==
+
+ @Override
+ public String toString()
+ {
+ return "RequestContext{" +
+ "this=" + super.toString() +
+ ", parent=" + parent +
+ '}';
+ }
}
@@ -34,20 +34,26 @@
private String requestPath;
/** Extra data associated with this request. */
- private RequestContext requestContext;
+ private final RequestContext requestContext;
/** Used internally by Routers. */
- private Stack<String> pathStack;
+ private final Stack<String> pathStack;
/** Used internally to track reposes where this request was */
- private List<String> processedRepositories;
+ private final List<String> processedRepositories;
/** Used internally to track applied mappins */
- private Map<String, List<String>> appliedMappings;
+ private final Map<String, List<String>> appliedMappings;
+ /**
+ * Constructor.
+ *
+ * @param requestPath the request path.
+ * @param localOnly See {@link RequestContext#CTX_LOCAL_ONLY_FLAG}.
+ * @param remoteOnly See {@link RequestContext#CTX_REMOTE_ONLY_FLAG}.
+ */
public ResourceStoreRequest( String requestPath, boolean localOnly, boolean remoteOnly )
{
- super();
this.requestPath = requestPath;
this.pathStack = new Stack<String>();
this.processedRepositories = new ArrayList<String>();
@@ -57,11 +63,23 @@ public ResourceStoreRequest( String requestPath, boolean localOnly, boolean remo
this.requestContext.setRequestRemoteOnly( remoteOnly );
}
+ /**
+ * Shortcut constructor.
+ *
+ * @param requestPath
+ * @param localOnly
+ * @deprecated use {@link #ResourceStoreRequest(String, boolean, boolean)} instead.
+ */
public ResourceStoreRequest( String requestPath, boolean localOnly )
{
this( requestPath, localOnly, false );
}
+ /**
+ * Shortcut constructor.
+ *
+ * @param requestPath
+ */
public ResourceStoreRequest( String requestPath )
{
this( requestPath, false, false );
@@ -71,37 +89,37 @@ public ResourceStoreRequest( String requestPath )
* Creates a request aimed at given path denoted by RepositoryItemUid.
*
* @param uid the uid
- * @deprecated use ResourceStoreRequest(String path)
+ * @deprecated use {@link #ResourceStoreRequest(String, boolean, boolean)} instead.
*/
public ResourceStoreRequest( RepositoryItemUid uid, boolean localOnly )
{
this( uid.getPath(), localOnly, false );
}
/**
- * Creates a request for a given item.
+ * Creates a request for a given item that is expected to be already present (locally).
*
* @param item
*/
- public ResourceStoreRequest( StorageItem item )
+ public ResourceStoreRequest( final StorageItem item )
{
this( item.getRepositoryItemUid().getPath(), true, false );
-
- this.requestContext = item.getItemContext();
+ getRequestContext().setParentContext( item.getItemContext() );
}
/**
- * Creates a new request off from a given one.
+ * Creates a new request off from a given one, item is expected to be already present (locally).
*
- * @param item
+ * @param request
*/
- public ResourceStoreRequest( ResourceStoreRequest request )
+ public ResourceStoreRequest( final ResourceStoreRequest request )
{
this( request.getRequestPath(), true, false );
-
getRequestContext().setParentContext( request.getRequestContext() );
}
+ // ==
+
/**
* Gets the request context.
*
@@ -127,9 +145,10 @@ public String getRequestPath()
*
* @param requestPath the new request path
*/
- public void setRequestPath( String requestPath )
+ public ResourceStoreRequest setRequestPath( String requestPath )
{
this.requestPath = requestPath;
+ return this;
}
/**
@@ -171,9 +190,10 @@ public boolean isRequestLocalOnly()
*
* @param requestLocalOnly the new request local only
*/
- public void setRequestLocalOnly( boolean requestLocalOnly )
+ public ResourceStoreRequest setRequestLocalOnly( boolean requestLocalOnly )
{
getRequestContext().setRequestLocalOnly( requestLocalOnly );
+ return this;
}
/**
@@ -189,11 +209,12 @@ public boolean isRequestRemoteOnly()
/**
* Sets the request remote only.
*
- * @param requestremoteOnly the new request remote only
+ * @param requestRemoteOnly the new request remote only
*/
- public void setRequestRemoteOnly( boolean requestRemoteOnly )
+ public ResourceStoreRequest setRequestRemoteOnly( boolean requestRemoteOnly )
{
getRequestContext().setRequestRemoteOnly( requestRemoteOnly );
+ return this;
}
/**
@@ -209,11 +230,12 @@ public boolean isRequestGroupLocalOnly()
/**
* Sets the request group local only.
*
- * @param requestremoteOnly the new request group local only
+ * @param requestGroupLocal the new request group local only
*/
- public void setRequestGroupLocalOnly( boolean requestGroupLocal )
+ public ResourceStoreRequest setRequestGroupLocalOnly( boolean requestGroupLocal )
{
getRequestContext().setRequestGroupLocalOnly( requestGroupLocal );
+ return this;
}
/**
@@ -261,9 +283,10 @@ public long getIfModifiedSince()
*
* @param ifModifiedSince
*/
- public void setIfModifiedSince( long ifModifiedSince )
+ public ResourceStoreRequest setIfModifiedSince( long ifModifiedSince )
{
getRequestContext().setIfModifiedSince( ifModifiedSince );
+ return this;
}
/**
@@ -281,9 +304,10 @@ public String getIfNoneMatch()
*
* @param tag
*/
- public void setIfNoneMatch( String tag )
+ public ResourceStoreRequest setIfNoneMatch( String tag )
{
getRequestContext().setIfNoneMatch( tag );
+ return this;
}
/**
@@ -301,9 +325,10 @@ public String getRequestUrl()
*
* @param url
*/
- public void setRequestUrl( String url )
+ public ResourceStoreRequest setRequestUrl( String url )
{
getRequestContext().setRequestUrl( url );
+ return this;
}
/**
@@ -321,9 +346,10 @@ public String getRequestAppRootUrl()
*
* @param url
*/
- public void setRequestAppRootUrl( String url )
+ public ResourceStoreRequest setRequestAppRootUrl( String url )
{
getRequestContext().setRequestAppRootUrl( url );
+ return this;
}
/**
@@ -349,10 +375,12 @@ public void addAppliedMappingsList( Repository repository, List<String> mappingL
@Override
public String toString()
{
- StringBuilder sb = new StringBuilder( getClass().getSimpleName() );
- sb.append( "(requestPath=\"" );
- sb.append( getRequestPath() );
- sb.append( "\")" );
- return sb.toString();
+ return "ResourceStoreRequest{" +
+ "requestPath='" + requestPath + '\'' +
+ ", requestContext=" + requestContext +
+ ", pathStack=" + pathStack +
+ ", processedRepositories=" + processedRepositories +
+ ", appliedMappings=" + appliedMappings +
+ '}';
}
}
@@ -80,7 +80,9 @@ public void storeItemWithChecksums( ResourceStoreRequest request, InputStream is
RepositoryStringUtils.getHumanizedNameString( getMavenRepository() ), request ), e );
}
- StorageFileItem storedFile = (StorageFileItem) getMavenRepository().retrieveItem( false, request );
+ // NXCM-4861: Doing "local only" lookup, same code should be used as in org.sonatype.nexus.proxy.repository.AbstractProxyRepository#doCacheItem
+ // Note: ResourceStoreRequest( ResourceStoreRequest ) creates a "subordinate" request from passed with same path but localOnly=true
+ StorageFileItem storedFile = (StorageFileItem) getMavenRepository().retrieveItem( false, new ResourceStoreRequest( request ) );
String sha1Hash = storedFile.getRepositoryItemAttributes().get( DigestCalculatingInspector.DIGEST_SHA1_KEY );
Oops, something went wrong.

0 comments on commit 1b147dd

Please sign in to comment.