Skip to content
This repository has been archived by the owner on Apr 20, 2022. It is now read-only.

Commit

Permalink
Fix #1 by defining default index file list which can be used to resol…
Browse files Browse the repository at this point in the history
…ve to file in case that information is missing
  • Loading branch information
stefan-sachs authored and Alexander Constantin committed Aug 6, 2015
1 parent 4f3be60 commit ace0110
Showing 1 changed file with 41 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright (C) 2015 Red Bull Media House GmbH <http://www.redbullmediahouse.com> - all rights reserved.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -18,6 +18,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
Expand Down Expand Up @@ -46,7 +47,9 @@
public class ApiDocController {

private Logger logger = LoggerFactory.getLogger(getClass());


private static final List<String> DEFAULT_INDEX_FILES = Arrays.asList("index.html", "index.htm");

@Autowired
private RepositoryService repositoryService;

Expand All @@ -60,9 +63,10 @@ private void ensureValidGroupId(String groupId) throws AccessNotAllowedException
}

private void addBasicAttributes(Model model, HttpServletRequest request) {

String baseUrl = request.getScheme() + "://" + (request.getHeader("Host") != null ? request.getHeader("Host") : "localhost");


String baseUrl = request.getScheme() + "://"
+ (request.getHeader("Host") != null ? request.getHeader("Host") : "localhost");

model.addAttribute("name", repositoryService.getName());
model.addAttribute("baseUrl", baseUrl);
model.addAttribute("defaultClassifier", repositoryService.getDefaultClassifier());
Expand All @@ -75,7 +79,7 @@ private void addBasicAttributes(Model model, HttpServletRequest request) {
String home(Model model, HttpServletRequest request) {

addBasicAttributes(model, request);

return "home";
}

Expand Down Expand Up @@ -137,13 +141,14 @@ void serve(@PathVariable String groupId,
version,
classifier,
subPath);
if (subPath.endsWith("/")) {}
ensureValidGroupId(groupId);

File jar = repositoryService.retrieveJarFile(groupId, artifactId, version, classifier);

if (jar == null) {
throw new RuntimeException("No documentation artifact file available for group:" + groupId + ", artifact:" + artifactId
+ " and version:" + version + "");
throw new RuntimeException("No documentation artifact file available for group:" + groupId + ", artifact:"
+ artifactId + " and version:" + version + "");
}
try {
serveFileFromJarFile(response, jar, subPath);
Expand All @@ -164,6 +169,32 @@ private void serveFileFromJarFile(HttpServletResponse response, File jar, String
return;
}

// fallback for requesting a directory without a trailing /
// this leads to a jarentry which is not null, and not a directory and of size 0
// this shouldn't be
if (!entry.isDirectory() && entry.getSize() == 0) {
if (!subPath.endsWith("/")) {
JarEntry entryWithSlash = jarFile.getJarEntry(subPath + "/");
if (entryWithSlash != null && entryWithSlash.isDirectory()) {
entry = entryWithSlash;
}
}
}

if (entry.isDirectory()) {
for (String indexFile : DEFAULT_INDEX_FILES) {
entry = jarFile.getJarEntry((subPath.endsWith("/") ? subPath : subPath + "/") + indexFile);
if (entry != null) {
break;
}
}
}

if (entry == null) {
response.sendError(404);
return;
}

response.setContentLength((int) entry.getSize());
String mimetype = getMimeType(entry.getName());
response.setContentType(mimetype);
Expand Down

0 comments on commit ace0110

Please sign in to comment.