Search in sources :

Example 1 with NotFoundException

use of spark.examples.exception.NotFoundException in project spark by perwendel.

the class StaticFilesTest method setup.

@BeforeClass
public static void setup() throws IOException {
    testUtil = new SparkTestUtil(4567);
    tmpExternalFile = new File(System.getProperty("java.io.tmpdir"), EXTERNAL_FILE_NAME_HTML);
    FileWriter writer = new FileWriter(tmpExternalFile);
    writer.write(CONTENT_OF_EXTERNAL_FILE);
    writer.flush();
    writer.close();
    staticFiles.location("/public");
    staticFiles.externalLocation(System.getProperty("java.io.tmpdir"));
    get("/hello", (q, a) -> FO_SHIZZY);
    get("/*", (q, a) -> {
        throw new NotFoundException();
    });
    exception(NotFoundException.class, (e, request, response) -> {
        response.status(404);
        response.body(NOT_FOUND_BRO);
    });
    Spark.awaitInitialization();
}
Also used : FileWriter(java.io.FileWriter) NotFoundException(spark.examples.exception.NotFoundException) SparkTestUtil(spark.util.SparkTestUtil) File(java.io.File) BeforeClass(org.junit.BeforeClass)

Example 2 with NotFoundException

use of spark.examples.exception.NotFoundException in project spark by perwendel.

the class StaticFilesTestExternal method setup.

@BeforeClass
public static void setup() throws IOException {
    testUtil = new SparkTestUtil(4567);
    String directoryRoot = System.getProperty("java.io.tmpdir") + "sparkish";
    new File(directoryRoot).mkdirs();
    tmpExternalFile1 = new File(directoryRoot, EXTERNAL_FILE_NAME_HTML);
    FileWriter writer = new FileWriter(tmpExternalFile1);
    writer.write(CONTENT_OF_EXTERNAL_FILE);
    writer.flush();
    writer.close();
    File root = new File(directoryRoot);
    folderOutsideStaticFiles = new File(root.getAbsolutePath() + "/../dumpsterstuff");
    folderOutsideStaticFiles.mkdirs();
    String newFilePath = root.getAbsolutePath() + "/../dumpsterstuff/Spark.class";
    tmpExternalFile2 = new File(newFilePath);
    tmpExternalFile2.createNewFile();
    staticFiles.externalLocation(directoryRoot);
    get("/hello", (q, a) -> FO_SHIZZY);
    get("/*", (q, a) -> {
        throw new NotFoundException();
    });
    exception(NotFoundException.class, (e, request, response) -> {
        response.status(404);
        response.body(NOT_FOUND_BRO);
    });
    Spark.awaitInitialization();
}
Also used : FileWriter(java.io.FileWriter) NotFoundException(spark.examples.exception.NotFoundException) SparkTestUtil(spark.util.SparkTestUtil) File(java.io.File) BeforeClass(org.junit.BeforeClass)

Example 3 with NotFoundException

use of spark.examples.exception.NotFoundException in project spark by perwendel.

the class GenericIntegrationTest method setup.

@BeforeClass
public static void setup() throws IOException {
    testUtil = new SparkTestUtil(4567);
    tmpExternalFile = new File(System.getProperty("java.io.tmpdir"), "externalFile.html");
    FileWriter writer = new FileWriter(tmpExternalFile);
    writer.write("Content of external file");
    writer.flush();
    writer.close();
    staticFileLocation("/public");
    externalStaticFileLocation(System.getProperty("java.io.tmpdir"));
    webSocket("/ws", WebSocketTestHandler.class);
    before("/secretcontent/*", (q, a) -> {
        halt(401, "Go Away!");
    });
    before("/protected/*", "application/xml", (q, a) -> {
        halt(401, "Go Away!");
    });
    before("/protected/*", "application/json", (q, a) -> {
        halt(401, "{\"message\": \"Go Away!\"}");
    });
    get("/hi", "application/json", (q, a) -> "{\"message\": \"Hello World\"}");
    get("/hi", (q, a) -> "Hello World!");
    get("/binaryhi", (q, a) -> "Hello World!".getBytes());
    get("/bytebufferhi", (q, a) -> ByteBuffer.wrap("Hello World!".getBytes()));
    get("/inputstreamhi", (q, a) -> new ByteArrayInputStream("Hello World!".getBytes("utf-8")));
    get("/param/:param", (q, a) -> "echo: " + q.params(":param"));
    path("/firstPath", () -> {
        before("/*", (q, a) -> a.header("before-filter-ran", "true"));
        get("/test", (q, a) -> "Single path-prefix works");
        path("/secondPath", () -> {
            get("/test", (q, a) -> "Nested path-prefix works");
            path("/thirdPath", () -> {
                get("/test", (q, a) -> "Very nested path-prefix works");
            });
        });
    });
    get("/paramandwild/:param/stuff/*", (q, a) -> "paramandwild: " + q.params(":param") + q.splat()[0]);
    get("/paramwithmaj/:paramWithMaj", (q, a) -> "echo: " + q.params(":paramWithMaj"));
    get("/templateView", (q, a) -> new ModelAndView("Hello", "my view"), new TemplateEngine() {

        @Override
        public String render(ModelAndView modelAndView) {
            return modelAndView.getModel() + " from " + modelAndView.getViewName();
        }
    });
    get("/", (q, a) -> "Hello Root!");
    post("/poster", (q, a) -> {
        String body = q.body();
        // created
        a.status(201);
        return "Body was: " + body;
    });
    post("/post_via_get", (q, a) -> {
        // created
        a.status(201);
        return "Method Override Worked";
    });
    get("/post_via_get", (q, a) -> "Method Override Did Not Work");
    patch("/patcher", (q, a) -> {
        String body = q.body();
        a.status(200);
        return "Body was: " + body;
    });
    get("/session_reset", (q, a) -> {
        String key = "session_reset";
        Session session = q.session();
        session.attribute(key, "11111");
        session.invalidate();
        session = q.session();
        session.attribute(key, "22222");
        return session.attribute(key);
    });
    get("/ip", (request, response) -> request.ip());
    after("/hi", (q, a) -> {
        if (q.requestMethod().equalsIgnoreCase("get")) {
            Assert.assertNotNull(a.body());
        }
        a.header("after", "foobar");
    });
    get("/throwexception", (q, a) -> {
        throw new UnsupportedOperationException();
    });
    get("/throwsubclassofbaseexception", (q, a) -> {
        throw new SubclassOfBaseException();
    });
    get("/thrownotfound", (q, a) -> {
        throw new NotFoundException();
    });
    get("/throwmeyling", (q, a) -> {
        throw new JWGmeligMeylingException();
    });
    exception(JWGmeligMeylingException.class, (meylingException, q, a) -> {
        a.body(meylingException.trustButVerify());
    });
    exception(UnsupportedOperationException.class, (exception, q, a) -> {
        a.body("Exception handled");
    });
    exception(BaseException.class, (exception, q, a) -> {
        a.body("Exception handled");
    });
    exception(NotFoundException.class, (exception, q, a) -> {
        a.status(404);
        a.body(NOT_FOUND_BRO);
    });
    get("/exception", (request, response) -> {
        throw new RuntimeException();
    });
    afterAfter("/exception", (request, response) -> {
        response.body("done executed for exception");
    });
    post("/nice", (request, response) -> "nice response");
    afterAfter("/nice", (request, response) -> {
        response.header("post-process", "nice done response");
    });
    afterAfter((request, response) -> {
        response.header("post-process-all", "nice done response after all");
    });
    Spark.awaitInitialization();
}
Also used : FileWriter(java.io.FileWriter) NotFoundException(spark.examples.exception.NotFoundException) ByteArrayInputStream(java.io.ByteArrayInputStream) SubclassOfBaseException(spark.examples.exception.SubclassOfBaseException) JWGmeligMeylingException(spark.examples.exception.JWGmeligMeylingException) SparkTestUtil(spark.util.SparkTestUtil) File(java.io.File) BeforeClass(org.junit.BeforeClass)

Example 4 with NotFoundException

use of spark.examples.exception.NotFoundException in project spark by perwendel.

the class StaticFilesMemberTest method setup.

@BeforeClass
public static void setup() throws IOException {
    testUtil = new SparkTestUtil(4567);
    tmpExternalFile = new File(System.getProperty("java.io.tmpdir"), EXTERNAL_FILE_NAME_HTML);
    FileWriter writer = new FileWriter(tmpExternalFile);
    writer.write(CONTENT_OF_EXTERNAL_FILE);
    writer.flush();
    writer.close();
    staticFiles.location("/public");
    staticFiles.externalLocation(System.getProperty("java.io.tmpdir"));
    get("/hello", (q, a) -> FO_SHIZZY);
    get("/*", (q, a) -> {
        throw new NotFoundException();
    });
    exception(NotFoundException.class, (e, request, response) -> {
        response.status(404);
        response.body(NOT_FOUND_BRO);
    });
    Spark.awaitInitialization();
}
Also used : FileWriter(java.io.FileWriter) NotFoundException(spark.examples.exception.NotFoundException) SparkTestUtil(spark.util.SparkTestUtil) File(java.io.File) BeforeClass(org.junit.BeforeClass)

Example 5 with NotFoundException

use of spark.examples.exception.NotFoundException in project spark by perwendel.

the class DisableMimeGuessingTest method setup.

@BeforeClass
public static void setup() throws IOException {
    testUtil = new SparkTestUtil(4567);
    tmpExternalFile = new File(System.getProperty("java.io.tmpdir"), EXTERNAL_FILE_NAME_HTML);
    FileWriter writer = new FileWriter(tmpExternalFile);
    writer.write(CONTENT_OF_EXTERNAL_FILE);
    writer.flush();
    writer.close();
    staticFiles.location("/public");
    staticFiles.externalLocation(System.getProperty("java.io.tmpdir"));
    staticFiles.disableMimeTypeGuessing();
    get("/hello", (q, a) -> FO_SHIZZY);
    get("/*", (q, a) -> {
        throw new NotFoundException();
    });
    Spark.awaitInitialization();
}
Also used : FileWriter(java.io.FileWriter) NotFoundException(spark.examples.exception.NotFoundException) SparkTestUtil(spark.util.SparkTestUtil) File(java.io.File) BeforeClass(org.junit.BeforeClass)

Aggregations

File (java.io.File)5 FileWriter (java.io.FileWriter)5 BeforeClass (org.junit.BeforeClass)5 NotFoundException (spark.examples.exception.NotFoundException)5 SparkTestUtil (spark.util.SparkTestUtil)5 ByteArrayInputStream (java.io.ByteArrayInputStream)1 JWGmeligMeylingException (spark.examples.exception.JWGmeligMeylingException)1 SubclassOfBaseException (spark.examples.exception.SubclassOfBaseException)1