ninja.NinjaController Maven / Gradle / Ivy
Show all versions of s3ninja Show documentation
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - [email protected]
*/
package ninja;
import com.google.common.collect.Maps;
import com.google.common.hash.HashCode;
import com.google.common.hash.Hashing;
import com.google.common.io.BaseEncoding;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpResponseStatus;
import sirius.kernel.commons.PriorityCollector;
import sirius.kernel.di.std.Part;
import sirius.kernel.di.std.Register;
import sirius.kernel.health.Exceptions;
import sirius.kernel.health.HandledException;
import sirius.web.controller.Controller;
import sirius.web.controller.Message;
import sirius.web.controller.Routed;
import sirius.web.http.MimeHelper;
import sirius.web.http.Response;
import sirius.web.http.WebContext;
import sirius.web.security.UserContext;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* Takes care of the "management ui".
*
* Handles all requests required to render the web-pages.
*/
@Register
public class NinjaController implements Controller {
@Override
public void onError(WebContext ctx, HandledException error) {
if (error != null) {
UserContext.message(Message.error(error.getMessage()));
}
List buckets = Collections.emptyList();
try {
buckets = storage.getBuckets();
} catch (HandledException e) {
UserContext.message(Message.error(e.getMessage()));
}
ctx.respondWith()
.template("view/index.html",
buckets,
storage.getBasePath(),
storage.getAwsAccessKey(),
storage.getAwsSecretKey());
}
@Part
private Storage storage;
@Part
private APILog log;
/**
* Handles requests to /
*
* @param ctx the context describing the current request
*/
@Routed("/")
public void index(WebContext ctx) {
if (ctx.isPOST() && ctx.get("bucketName").isFilled()) {
storage.getBucket(ctx.get("bucketName").asString()).create();
UserContext.message(Message.info("Bucket successfully created."));
}
onError(ctx, null);
}
/**
* Handles requests to /ui/license
*
* @param ctx the context describing the current request
*/
@Routed(value = "/ui/license", priority = PriorityCollector.DEFAULT_PRIORITY - 1)
public void license(WebContext ctx) {
ctx.respondWith().template("view/license.html");
}
/**
* Handles requests to /ui/api
*
* @param ctx the context describing the current request
*/
@Routed(value = "/ui/api", priority = PriorityCollector.DEFAULT_PRIORITY - 1)
public void api(WebContext ctx) {
ctx.respondWith().template("view/api.html");
}
/**
* Handles requests to /ui/log
*
* @param ctx the context describing the current request
*/
@Routed(value = "/ui/log", priority = PriorityCollector.DEFAULT_PRIORITY - 1)
public void log(WebContext ctx) {
int start = ctx.get("start").asInt(1) - 1;
int pageSize = 50;
List entries = log.getEntries(start, pageSize + 1);
boolean canPagePrev = start > 0;
boolean canPageNext = entries.size() > pageSize;
if (canPageNext) {
entries.remove(entries.size() - 1);
}
ctx.respondWith()
.template("view/log.html",
entries,
canPagePrev,
canPageNext,
(start + 1) + " - " + (start + entries.size()),
Math.max(1, start - pageSize + 1),
start + pageSize + 1);
}
/**
* Handles requests to /ui/[bucketName]
*
* This will list the contents of the bucket.
*
*
* @param ctx the context describing the current request
* @param bucketName name of the bucket to show
*/
@Routed("/ui/:1")
public void bucket(WebContext ctx, String bucketName) {
Bucket bucket = storage.getBucket(bucketName);
ctx.respondWith().template("view/bucket.html", bucket);
}
/**
* Handles requests to /ui/[bucketName]/[object]
*
* This will start a download of the requested object. No access checks will be performed.
*
*
* @param ctx the context describing the current request
* @param bucketName name of the bucket to show
* @param id the name of the object to fetch
*/
@Routed("/ui/:1/:2")
public void object(WebContext ctx, String bucketName, String id) {
try {
Bucket bucket = storage.getBucket(bucketName);
if (!bucket.exists()) {
ctx.respondWith().error(HttpResponseStatus.NOT_FOUND, "Bucket does not exist");
return;
}
StoredObject object = bucket.getObject(id);
if (!object.exists()) {
ctx.respondWith().error(HttpResponseStatus.NOT_FOUND, "Object does not exist");
return;
}
Response response = ctx.respondWith();
for (Map.Entry