Skip to content

Commit

Permalink
Use frontend layout in not found error page (Resolve #15) and support…
Browse files Browse the repository at this point in the history
… main repository in path (Resolve #14)
  • Loading branch information
dzikoysk committed May 7, 2020
1 parent 492f9f0 commit 6eed866
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 33 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Releases: [GitHub Downloads](https://github.com/dzikoysk/NanoMaven/releases)
List of available management commands

```bash
NanoMaven 2.0.0 Commands:
NanoMaven 2.0.1 Commands:
help - List available commands
tokens - List all generated tokens
keygen <path> <alias> - Generate a new access token for the given path
Expand Down Expand Up @@ -96,7 +96,7 @@ You can also use maven builds to embed NanoMaven in your application
<dependency>
<groupId>org.panda-lang</groupId>
<artifactId>nanomaven</artifactId>
<version>2.0.0</version>
<version>2.0.1</version>
</dependency>
<repository>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

<groupId>org.panda-lang</groupId>
<artifactId>nanomaven</artifactId>
<version>2.0.0</version>
<version>2.0.1</version>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/panda_lang/nanomaven/NanoConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

public final class NanoConstants {

public static final String VERSION = "2.0.0";
public static final String VERSION = "2.0.1";

static final String GREETING_MESSAGE = ansi().bold().fg(Color.GREEN).a("NanoMaven ").reset().a(NanoConstants.VERSION).toString();

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/panda_lang/nanomaven/frontend/Frontend.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.panda_lang.nanomaven.frontend;

import org.panda_lang.utilities.commons.StringUtils;

public final class Frontend {

private final String content;
Expand All @@ -24,8 +26,8 @@ public Frontend(String content) {
this.content = content;
}

public String getContent() {
return content;
public String forMessage(String message) {
return StringUtils.replace(content, "{{message}}", message);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public FrontendController(NanoMaven nanoMaven) {

@Override
public NanoHTTPD.Response serve(NanoHttpServer server, NanoHTTPD.IHTTPSession session) {
return NanoHTTPD.newFixedLengthResponse(nanoMaven.getFrontend().getContent());
return NanoHTTPD.newFixedLengthResponse(nanoMaven.getFrontend().forMessage("maven repository"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,37 +34,39 @@ public class DownloadController implements NanoController {
public NanoHTTPD.Response serve(NanoHttpServer server, NanoHTTPD.IHTTPSession session) {
NanoMaven nanoMaven = server.getNanoMaven();
RepositoryService repositoryService = nanoMaven.getRepositoryService();
String uri = session.getUri();

String[] path = session.getUri().replace("maven-metadata", "maven-metadata-local").split("/");
if (!nanoMaven.getConfiguration().isRepositoryPathEnabled()) {
String mainRepository = nanoMaven.getConfiguration().getRepositories().get(0);

if (path[0].isEmpty()) {
path = Arrays.copyOfRange(path, 1, path.length);
if (!uri.startsWith("/" + mainRepository)) {
uri = nanoMaven.getConfiguration().getRepositories().get(0) + "/" + uri;
}
}

Artifact project;
String[] path = uri.replace("maven-metadata", "maven-metadata-local").split("/");

if (!nanoMaven.getConfiguration().isRepositoryPathEnabled()) {
project = repositoryService.find(path);
if (path[0].isEmpty()) {
path = Arrays.copyOfRange(path, 1, path.length);
}
else {
Repository repository = repositoryService.getRepository(path[0]);

if (repository == null) {
return NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.NOT_FOUND, "text/html", "Repository " + path[0] + " not found");
}
Repository repository = repositoryService.getRepository(path[0]);

project = repository.get(Arrays.copyOfRange(path, 1, path.length));
if (repository == null) {
return notFound(nanoMaven, "Repository " + path[0] + " not found");
}

if (project == null) {
return NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.NOT_FOUND, "text/html", "Artifact not found");
Artifact artifact = repository.get(Arrays.copyOfRange(path, 1, path.length));

if (artifact == null) {
return notFound(nanoMaven, "Artifact not found");
}

File file = project.getFile(path[path.length - 1]);
File file = artifact.getFile(path[path.length - 1]);

if (!file.exists()) {
NanoMaven.getLogger().warn("File " + file.getAbsolutePath() + " doesn't exist");
return NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.NOT_FOUND, "text/html", "Artifact " + file.getName() + " not found");
return notFound(nanoMaven, "Artifact " + file.getName() + " not found");
}

FileInputStream fis = null;
Expand All @@ -77,21 +79,23 @@ public NanoHTTPD.Response serve(NanoHttpServer server, NanoHTTPD.IHTTPSession se

if (fis == null) {
NanoMaven.getLogger().warn("Cannot read file " + file.getAbsolutePath());
return NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.NOT_FOUND, "text/html", "Cannot read artifact");
return notFound(nanoMaven, "Cannot read artifact");
}

NanoHTTPD.Response response = NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.NOT_FOUND, "text/html", "Unknown mime type");

try {
response = NanoHTTPD.newChunkedResponse(NanoHTTPD.Response.Status.OK, Files.probeContentType(file.toPath()), fis);
NanoHTTPD.Response response = NanoHTTPD.newChunkedResponse(NanoHTTPD.Response.Status.OK, Files.probeContentType(file.toPath()), fis);
response.addHeader("Content-Disposition", "attachment; filename=\"" + file.getName().replace("maven-metadata-local", "maven-metadata") +"\"");
response.addHeader("Content-Length", String.valueOf(file.length()));
NanoMaven.getLogger().info("Fis: " + fis.available() + "; mime: " + Files.probeContentType(file.toPath()));
return response;
} catch (IOException e) {
e.printStackTrace();
return notFound(nanoMaven, "Unknown mime type");
}
}

return response;
private NanoHTTPD.Response notFound(NanoMaven nanoMaven, String message) {
return NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.NOT_FOUND, "text/html", nanoMaven.getFrontend().forMessage(message));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@

package org.panda_lang.nanomaven.repository;

import org.panda_lang.nanomaven.NanoMaven;
import org.panda_lang.nanomaven.NanoConfiguration;
import org.panda_lang.nanomaven.NanoMaven;

import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class RepositoryService {

private final Map<String, Repository> repositories;

public RepositoryService() {
this.repositories = new HashMap<>(2);
this.repositories = new LinkedHashMap<>(2);
}

public void scan(NanoConfiguration configuration) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<body>
<main>
<h1>#onlypanda</h1>
<p>~ maven repository ~</p>
<p>~ {{message}} ~</p>
</main>
</body>
</html>
3 changes: 2 additions & 1 deletion src/main/resources/nanomaven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ repositoryPathEnabled: false
# Enable directory indexing
indexingEnabled: true

# Root directories of repositories
# Root directories of repositories.
# First directory on the list is the main repository.
repositories:
- releases
- snapshots
Expand Down

0 comments on commit 6eed866

Please sign in to comment.