Skip to content

Commit 5435366

Browse files
committed
Fix the NPM listing
1 parent 3c9fac1 commit 5435366

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

addons/content-browse/common/src/main/java/org/commonjava/indy/content/browse/ContentBrowseController.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
*/
1616
package org.commonjava.indy.content.browse;
1717

18+
import org.commonjava.cdi.util.weft.ThreadContext;
1819
import org.commonjava.indy.IndyWorkflowException;
1920
import org.commonjava.indy.content.ContentManager;
2021
import org.commonjava.indy.content.StoreResource;
2122
import org.commonjava.indy.content.browse.model.ContentBrowseResult;
2223
import org.commonjava.indy.data.IndyDataException;
2324
import org.commonjava.indy.data.StoreDataManager;
25+
import org.commonjava.indy.util.RequestContextHelper;
2426
import org.commonjava.o11yphant.metrics.annotation.Measure;
2527
import org.commonjava.indy.model.core.ArtifactStore;
2628
import org.commonjava.indy.model.core.StoreKey;
@@ -43,6 +45,7 @@
4345
import java.util.Set;
4446
import java.util.TreeMap;
4547

48+
import static org.commonjava.indy.pkg.npm.model.NPMPackageTypeDescriptor.NPM_METADATA_NAME;
4649
import static org.commonjava.maven.galley.util.PathUtils.normalize;
4750
import static org.commonjava.maven.galley.util.PathUtils.parentPath;
4851

@@ -89,7 +92,13 @@ private ContentBrowseResult renderResult( final StoreKey key, final String reque
8992
String path = requestPath.endsWith( "/" ) ? requestPath : requestPath + "/";
9093

9194
final ArtifactStore store = getStore( key );
95+
96+
// This is used to let galley ignore the NPMPathStorageCalculator handling,
97+
// which will append package.json to a directory transfer and make listing not applicable.
98+
ThreadContext context = ThreadContext.getContext( true );
99+
context.put( RequestContextHelper.IS_RAW_VIEW, Boolean.TRUE );
92100
final List<StoreResource> listed = contentManager.list( store, path, eventMetadata );
101+
context.put( RequestContextHelper.IS_RAW_VIEW, Boolean.FALSE );
93102

94103
final Map<String, Set<String>> listingUrls = new TreeMap<>();
95104

@@ -115,6 +124,11 @@ private ContentBrowseResult renderResult( final StoreKey key, final String reque
115124
if ( p.endsWith( "-" ) || p.endsWith( "-/" ) )
116125
{
117126
//skip npm adduser path to avoid the sensitive info showing.
127+
//continue;
128+
}
129+
if ( p.endsWith( NPM_METADATA_NAME ) )
130+
{
131+
//the package.json should not be visible for user
118132
continue;
119133
}
120134
else if ( pass == 1 )

addons/content-browse/ftests/src/main/java/org/commonjava/indy/content/browse/ftest/StoreNPMThenListingDirTest.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,22 @@
1616
package org.commonjava.indy.content.browse.ftest;
1717

1818
import org.apache.commons.io.IOUtils;
19+
import org.commonjava.indy.client.core.IndyClientModule;
20+
import org.commonjava.indy.content.browse.client.IndyContentBrowseClientModule;
21+
import org.commonjava.indy.content.browse.model.ContentBrowseResult;
1922
import org.commonjava.indy.ftest.core.AbstractContentManagementTest;
2023
import org.commonjava.indy.model.core.HostedRepository;
2124
import org.commonjava.indy.model.core.StoreKey;
25+
import org.commonjava.indy.model.core.io.IndyObjectMapper;
2226
import org.commonjava.indy.pkg.maven.model.MavenPackageTypeDescriptor;
2327
import org.commonjava.indy.pkg.npm.model.NPMPackageTypeDescriptor;
2428
import org.junit.Test;
2529

2630
import java.io.ByteArrayInputStream;
2731
import java.io.InputStream;
2832
import java.net.URL;
33+
import java.util.Collection;
34+
import java.util.Collections;
2935

3036
import static org.commonjava.indy.model.core.StoreType.hosted;
3137
import static org.hamcrest.CoreMatchers.equalTo;
@@ -78,12 +84,20 @@ public void run()
7884

7985
final String dirPath = "/@babel/opossum";
8086
final String path = dirPath + "/package.json";
87+
final String tarPath = dirPath + "/-/opossum-5.0.0.tgz";
8188

8289
final StoreKey testKey = hostedRepository.getKey();
8390

91+
assertThat( client.content().exists( testKey, tarPath ), equalTo( false ) );
92+
93+
client.content().store( testKey, tarPath, stream );
94+
95+
assertThat( client.content().exists( testKey, tarPath ), equalTo( true ) );
96+
97+
final InputStream stream2 = new ByteArrayInputStream( content.getBytes() );
8498
assertThat( client.content().exists( testKey, path ), equalTo( false ) );
8599

86-
client.content().store( testKey, path, stream );
100+
client.content().store( testKey, path, stream2 );
87101

88102
assertThat( client.content().exists( testKey, path ), equalTo( true ) );
89103

@@ -103,8 +117,29 @@ public void run()
103117
assertThat( json.contains( "/@babel/" ), equalTo( true ) );
104118
}
105119

120+
IndyContentBrowseClientModule browseClientModule = client.module( IndyContentBrowseClientModule.class );
121+
122+
ContentBrowseResult browseResult = browseClientModule.getContentList(testKey, "/@babel/opossum");
123+
String browseResultInJson = new IndyObjectMapper(false).writeValueAsString(browseResult);
124+
125+
assertThat( browseResultInJson.contains("/@babel/opossum/-/"), equalTo(true) );
126+
assertThat( "no metadata result", browseResult, notNullValue() );
127+
128+
try(InputStream jsonIn = client.content().get( testKey, "/@babel/opossum" ))
129+
{
130+
assertThat( jsonIn, notNullValue() );
131+
String json = IOUtils.toString( jsonIn );
132+
assertEquals(content, json);
133+
}
134+
106135
assertThat( client.content()
107136
.exists( testKey, path ), equalTo( true ) );
108137

109138
}
139+
140+
@Override
141+
protected Collection<IndyClientModule> getAdditionalClientModules()
142+
{
143+
return Collections.singletonList( new IndyContentBrowseClientModule() );
144+
}
110145
}

0 commit comments

Comments
 (0)