All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.elepy.admin.Resource Maven / Gradle / Ivy

There is a newer version: 3.0.0-alpha-29
Show newest version
package com.elepy.admin;

import com.elepy.exceptions.ElepyException;
import com.elepy.http.HttpMethod;
import com.elepy.http.Route;
import com.elepy.http.RouteBuilder;
import org.apache.commons.io.IOUtils;

import java.io.InputStream;
import java.util.Objects;

public class Resource {
    private final String path;
    private final String contentType;
    private final String fileLocation;


    public Resource(String path, String contentType, String raw) {
        this.path = path;
        this.contentType = contentType;
        this.fileLocation = raw;
    }

    public Route toRoute() {
        return RouteBuilder.anElepyRoute().acceptType(contentType).method(HttpMethod.GET).path(path)
                .route(context -> {
                    context.type(contentType);
                    context.response().header("Content-Encoding", "gzip");
                    final var resourceAsStream = getClass().getClassLoader().getResourceAsStream(fileLocation);

                    if(resourceAsStream == null){
                        throw new ElepyException(String.format("Resource '%s' not found", fileLocation));
                    }
                    context.response().result(resourceAsStream);
                }).build();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy