Java Code Examples for org.sonatype.nexus.common.app.BaseUrlHolder#unset()

The following examples show how to use org.sonatype.nexus.common.app.BaseUrlHolder#unset() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MavenTestHelper.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private String url(final Repository repo, final String path) {
  boolean reset = false;
  if (!BaseUrlHolder.isSet()) {
    reset = true;
    BaseUrlHolder.set(nexusUrl.toString());
  }
  String repoPath = path.startsWith("/") ? path : '/' + path;
  try {
    return String.format("%s%s", repo.getUrl(), repoPath);
  }
  finally {
    if (reset) {
      BaseUrlHolder.unset();
    }
  }
}
 
Example 2
Source File: NpmMetadataUtilsTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void rewriteTarballUrlTest() {
  try {
    assertThat(BaseUrlHolder.isSet(), is(false));
    BaseUrlHolder.set("http://localhost:8080/");

    NestedAttributesMap packageRoot = new NestedAttributesMap("package", Maps.newHashMap());
    packageRoot.set("_id", "id");
    packageRoot.set("name", "package");
    packageRoot.child("versions").child("1.0").set("name", "package");
    packageRoot.child("versions").child("1.0").set("version", "1.0");
    packageRoot.child("versions").child("1.0").child("dist")
        .set("tarball", "http://example.com/path/package-1.0.tgz");
    packageRoot.child("versions").set("2.0", "incomplete");

    NpmMetadataUtils.rewriteTarballUrl("testRepo", packageRoot);

    String rewritten = packageRoot.child("versions").child("1.0").child("dist").get("tarball", String.class);
    assertThat(rewritten, equalTo("http://localhost:8080/repository/testRepo/package/-/package-1.0.tgz"));
  }
  finally {
    BaseUrlHolder.unset();
  }
}
 
Example 3
Source File: NpmPackageRootMetadataUtilsTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Before
public void setUp() {
  // reset for every request
  BaseUrlHolder.unset();
    
  when(packageRootAsset.name()).thenReturn("@foo/bar");
  when(packageRootAsset.requireBlobRef()).thenReturn(packageRootBlobRef);
  when(packageRootAsset.formatAttributes()).thenReturn(new NestedAttributesMap("metadata", new HashMap<>()));
  when(packageRootAsset.getEntityMetadata())
      .thenReturn(new DetachedEntityMetadata(new DetachedEntityId("foo"), new DetachedEntityVersion("a")));

  when(storageTx.requireBlob(packageRootBlobRef)).thenReturn(packageRootBlob);
  when(storageTx.findAssetWithProperty(eq(P_NAME), eq("@foo/bar"), any(Bucket.class))).thenReturn(packageRootAsset);
  when(packageRootBlob.getInputStream())
      .thenReturn(new ByteArrayInputStream(("{\"" + DIST_TAGS + "\": {\"latest\":\"1.5.3\"}}").getBytes()));

  UnitOfWork.beginBatch(storageTx);
}
 
Example 4
Source File: NpmPackageRootMetadataUtilsTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private void createFullPackageMetadataImpl(final BiFunction<String, String, String> function, String packageJsonFilename) throws URISyntaxException, IOException {
  try {
    assertThat(BaseUrlHolder.isSet(), is(false));
    BaseUrlHolder.set("http://localhost:8080/");

    File packageJsonFile = new File(NpmPackageRootMetadataUtilsTest.class.getResource(packageJsonFilename).toURI());
    File archive = tempFolderRule.newFile();
    Map<String, Object> packageJson = new NpmPackageParser()
        .parsePackageJson(() -> ArchiveUtils.pack(archive, packageJsonFile, "package/package.json"));

    NestedAttributesMap packageMetadata = NpmPackageRootMetadataUtils
        .createFullPackageMetadata(new NestedAttributesMap("metadata", packageJson),
            "npm-hosted",
            "abcd",
            repository,
            function);

    assertPackageMetadata(packageMetadata);
  }
  finally {
    BaseUrlHolder.unset();
  }
}
 
Example 5
Source File: RepositoryAuditor.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private Map<String, Object> createFullAttributes(final Repository repository) {
  boolean baseUrlAbsent = !BaseUrlHolder.isSet();
  try {
    if (baseUrlAbsent) {
      BaseUrlHolder.set(""); // use empty base URL placeholder during conversion to avoid log-spam
    }

    AbstractApiRepository apiObject = convert(repository);

    ObjectWriter writer = mapper.writerFor(apiObject.getClass());

    String json = writer.writeValueAsString(apiObject);

    return mapper.readerFor(new TypeReference<Map<String, Object>>()
    {
    }).readValue(json);
  }
  catch (Exception e) {
    log.error("Failed to convert repo object falling back to simple", e);
    return createSimple(repository);
  }
  finally {
    if (baseUrlAbsent) {
      BaseUrlHolder.unset();
    }
  }
}
 
Example 6
Source File: NpmMetadataUtilsTest.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@Before
public void setUp() {
  BaseUrlHolder.unset();
}
 
Example 7
Source File: NpmStreamingObjectMapperTest.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@Before
public void setUp() {
  BaseUrlHolder.unset();
}