Search in sources :

Example 1 with OrchidResponse

use of com.eden.orchid.api.server.OrchidResponse in project Orchid by JavaEden.

the class AdminAssetResponse method getResponse.

static OrchidResponse getResponse(OrchidContext context, File targetFile, String targetPath) {
    AdminTheme theme = context.getAdminTheme();
    OrchidResource res = theme.getResourceSource().getResourceEntry(context, targetPath);
    String mimeType = StaticFileResponse.mimeTypes.getOrDefault(FilenameUtils.getExtension(targetFile.getName()), "text/plain");
    Clog.i("Rendering admin File: #{$1}", targetPath);
    if (res != null) {
        if (context.isBinaryExtension(FilenameUtils.getExtension(targetFile.getName()))) {
            InputStream stream = res.getContentStream();
            return new OrchidResponse(context).contentStream(stream).mimeType(mimeType);
        } else {
            OrchidPage page = new OrchidPage(res, RenderService.RenderMode.TEMPLATE, "", null);
            return new OrchidResponse(context).mimeType(mimeType).content(page.getContent()).mimeType(mimeType);
        }
    }
    return null;
}
Also used : OrchidPage(com.eden.orchid.api.theme.pages.OrchidPage) OrchidResponse(com.eden.orchid.api.server.OrchidResponse) InputStream(java.io.InputStream) OrchidResource(com.eden.orchid.api.resources.resource.OrchidResource) AdminTheme(com.eden.orchid.api.theme.AdminTheme)

Example 2 with OrchidResponse

use of com.eden.orchid.api.server.OrchidResponse in project Orchid by JavaEden.

the class NotFound404Response method getResponse.

static OrchidResponse getResponse(OrchidContext context, String targetPath) {
    String content = "";
    Clog.i("Rendering 404: #{$1}", targetPath);
    OrchidResource resource = context.getFlexibleResourceSource(LocalResourceSource.INSTANCE, null).locateResourceEntry(context, "404");
    OrchidPage page = null;
    if (resource != null) {
        page = new OrchidPage(resource, RenderService.RenderMode.TEMPLATE, "404", "Not Found!");
    } else {
        resource = context.getDefaultResourceSource(null, null).getResourceEntry(context, "templates/server/404.peb");
        Map<String, Object> indexPageVars = new HashMap<>();
        indexPageVars.put("title", "Not Found - " + targetPath);
        indexPageVars.put("path", targetPath);
        Map<String, Object> object = new HashMap<>(context.getConfig());
        object.put("page", indexPageVars);
        object.put("theme", context.getTheme());
        String notFoundIndexContent;
        if (resource != null) {
            notFoundIndexContent = context.compile(resource, resource.getReference().getExtension(), resource.getContent(), object);
        } else {
            notFoundIndexContent = context.serialize("json", object);
        }
        page = new OrchidPage(new StringResource(new OrchidReference(context, "404.txt"), notFoundIndexContent), RenderService.RenderMode.TEMPLATE, "404", "Not Found!");
    }
    return new OrchidResponse(context).page(page).status(404);
}
Also used : OrchidPage(com.eden.orchid.api.theme.pages.OrchidPage) StringResource(com.eden.orchid.api.resources.resource.StringResource) OrchidReference(com.eden.orchid.api.theme.pages.OrchidReference) HashMap(java.util.HashMap) OrchidResponse(com.eden.orchid.api.server.OrchidResponse) OrchidResource(com.eden.orchid.api.resources.resource.OrchidResource)

Example 3 with OrchidResponse

use of com.eden.orchid.api.server.OrchidResponse in project Orchid by JavaEden.

the class IndexFileResponse method getResponse.

// TODO: convert this to a component
static OrchidResponse getResponse(OrchidContext context, File targetFile, String targetPath) {
    if (targetFile.isDirectory()) {
        Clog.i("Rendering directory index: {}", targetPath);
        File[] files = targetFile.listFiles();
        if (files != null) {
            List<FileRow> jsonDirs = new ArrayList<>();
            List<FileRow> jsonFiles = new ArrayList<>();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd-hh:mm:ss z", Locale.getDefault());
            jsonDirs.add(new FileRow(OrchidUtils.applyBaseUrl(context, StringUtils.strip(targetPath, "/")) + "/..", StringUtils.strip(targetPath, "/") + "/..", ".. (Go up)", "", "", "assets/svg/folder.svg"));
            for (File file : files) {
                FileRow newFile = new FileRow();
                newFile.url = OrchidUtils.applyBaseUrl(context, StringUtils.strip(targetPath, "/") + "/" + file.getName());
                newFile.path = StringUtils.strip(targetPath, "/") + "/" + file.getName();
                newFile.name = file.getName();
                newFile.date = formatter.format(new Date(file.lastModified()));
                String icon;
                if (file.isDirectory()) {
                    icon = "folder";
                    newFile.size = file.listFiles().length + " entries";
                    jsonDirs.add(newFile);
                } else {
                    icon = FilenameUtils.getExtension(file.getName());
                    newFile.size = humanReadableByteCount(file.length(), true);
                    jsonFiles.add(newFile);
                }
                newFile.icon = "assets/svg/" + icon + ".svg";
            }
            jsonDirs.sort(Comparator.comparing(fileRow -> fileRow.name));
            jsonFiles.sort(Comparator.comparing(fileRow -> fileRow.name));
            OrchidResource resource = context.getDefaultResourceSource(null, context.getTheme()).getResourceEntry(context, "templates/server/directoryListing.peb");
            Map<String, Object> indexPageVars = new HashMap<>();
            indexPageVars.put("title", "List of files/dirs under " + targetPath);
            indexPageVars.put("path", targetPath);
            indexPageVars.put("dirs", jsonDirs);
            indexPageVars.put("files", jsonFiles);
            Map<String, Object> object = new HashMap<>(context.getConfig());
            object.put("page", indexPageVars);
            object.put("theme", context.getTheme());
            String directoryListingContent;
            if (resource != null) {
                directoryListingContent = context.compile(resource, resource.getReference().getExtension(), resource.getContent(), object);
            } else {
                directoryListingContent = context.serialize("json", object);
            }
            OrchidPage page = new OrchidPage(new StringResource(new OrchidReference(context, "directoryListing.txt"), directoryListingContent), RenderService.RenderMode.TEMPLATE, "directoryListing", "Index");
            return new OrchidResponse(context).page(page).status(404);
        }
    }
    return new OrchidResponse(context).content("");
}
Also used : OrchidResponse(com.eden.orchid.api.server.OrchidResponse) OrchidResource(com.eden.orchid.api.resources.resource.OrchidResource) Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) RenderService(com.eden.orchid.api.render.RenderService) OrchidReference(com.eden.orchid.api.theme.pages.OrchidReference) StringUtils(org.apache.commons.lang3.StringUtils) OrchidContext(com.eden.orchid.api.OrchidContext) File(java.io.File) ArrayList(java.util.ArrayList) List(java.util.List) AssetPage(com.eden.orchid.api.theme.assets.AssetPage) Locale(java.util.Locale) Map(java.util.Map) AssetHolder(com.eden.orchid.api.theme.assets.AssetHolder) OrchidUtils(com.eden.orchid.utilities.OrchidUtils) Clog(com.caseyjbrooks.clog.Clog) AssetManagerDelegate(com.eden.orchid.api.theme.assets.AssetManagerDelegate) Comparator(java.util.Comparator) StringResource(com.eden.orchid.api.resources.resource.StringResource) FilenameUtils(org.apache.commons.io.FilenameUtils) OrchidPage(com.eden.orchid.api.theme.pages.OrchidPage) OrchidPage(com.eden.orchid.api.theme.pages.OrchidPage) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Date(java.util.Date) StringResource(com.eden.orchid.api.resources.resource.StringResource) OrchidReference(com.eden.orchid.api.theme.pages.OrchidReference) OrchidResponse(com.eden.orchid.api.server.OrchidResponse) OrchidResource(com.eden.orchid.api.resources.resource.OrchidResource) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat)

Example 4 with OrchidResponse

use of com.eden.orchid.api.server.OrchidResponse in project Orchid by JavaEden.

the class FileController method findFile.

@Override
public OrchidResponse findFile(OrchidContext context, String targetPath) {
    if (this.rootFolder == null) {
        this.rootFolder = new File(this.destination);
    }
    File targetFile = new File(rootFolder, targetPath);
    if (targetFile.exists()) {
        if (targetFile.isDirectory()) {
            for (String indexFile : indexFiles) {
                String indexPath = StringUtils.strip(targetPath, "/") + "/" + indexFile;
                File targetIndexFile = new File(rootFolder, indexPath.replace('/', File.separatorChar));
                if (targetIndexFile.exists()) {
                    return StaticFileResponse.getResponse(context, targetIndexFile, indexPath);
                }
            }
            return IndexFileResponse.getResponse(context, targetFile, targetPath);
        } else {
            return StaticFileResponse.getResponse(context, targetFile, targetPath);
        }
    } else {
        OrchidResponse adminAsset = AdminAssetResponse.getResponse(context, targetFile, targetPath);
        if (adminAsset != null) {
            return adminAsset;
        } else {
            return NotFound404Response.getResponse(context, targetPath);
        }
    }
}
Also used : OrchidResponse(com.eden.orchid.api.server.OrchidResponse) File(java.io.File)

Example 5 with OrchidResponse

use of com.eden.orchid.api.server.OrchidResponse in project Orchid by JavaEden.

the class FileController method findPage.

@Override
public OrchidResponse findPage(OrchidContext context, String targetPath) {
    List<String> possibleMatches = CollectionsKt.listOf(targetPath, targetPath + "/" + indexFiles[0], targetPath + "/" + indexFiles[1]);
    OrchidPage matchedPage = null;
    for (String possibleMatch : possibleMatches) {
        Stream<OrchidPage> indexedPagesStream = context.getIndex().getAllIndexedPages().values().stream().flatMap(it -> it.getFirst().getAllPages().stream());
        Stream<AssetPage> assetPagesStream = context.getAssetManager().getAllAssetPages();
        Stream<OrchidPage> allOrchidPagesStream = Stream.concat(indexedPagesStream, assetPagesStream);
        OrchidPage page = OrchidExtensionsKt.findPageByServerPath(allOrchidPagesStream, possibleMatch);
        if (page != null) {
            matchedPage = page;
            break;
        }
    }
    if (matchedPage != null) {
        return new OrchidResponse(context).page(matchedPage).status(200);
    } else {
        return findFile(context, targetPath);
    }
}
Also used : OrchidPage(com.eden.orchid.api.theme.pages.OrchidPage) OrchidResponse(com.eden.orchid.api.server.OrchidResponse) AssetPage(com.eden.orchid.api.theme.assets.AssetPage)

Aggregations

OrchidResponse (com.eden.orchid.api.server.OrchidResponse)5 OrchidPage (com.eden.orchid.api.theme.pages.OrchidPage)4 OrchidResource (com.eden.orchid.api.resources.resource.OrchidResource)3 StringResource (com.eden.orchid.api.resources.resource.StringResource)2 AssetPage (com.eden.orchid.api.theme.assets.AssetPage)2 OrchidReference (com.eden.orchid.api.theme.pages.OrchidReference)2 File (java.io.File)2 HashMap (java.util.HashMap)2 Clog (com.caseyjbrooks.clog.Clog)1 OrchidContext (com.eden.orchid.api.OrchidContext)1 RenderService (com.eden.orchid.api.render.RenderService)1 AdminTheme (com.eden.orchid.api.theme.AdminTheme)1 AssetHolder (com.eden.orchid.api.theme.assets.AssetHolder)1 AssetManagerDelegate (com.eden.orchid.api.theme.assets.AssetManagerDelegate)1 OrchidUtils (com.eden.orchid.utilities.OrchidUtils)1 InputStream (java.io.InputStream)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 Date (java.util.Date)1