leola.web.WebLeolaLibrary.java Source code

Java tutorial

Introduction

Here is the source code for leola.web.WebLeolaLibrary.java

Source

/*
 * see license.txt
 */
package leola.web;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;

import leola.vm.Leola;
import leola.vm.exceptions.LeolaRuntimeException;
import leola.vm.lib.LeolaIgnore;
import leola.vm.lib.LeolaLibrary;
import leola.vm.lib.LeolaMethod;
import leola.vm.types.LeoArray;
import leola.vm.types.LeoMap;
import leola.vm.types.LeoNamespace;
import leola.vm.types.LeoObject;
import leola.vm.types.LeoString;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;

/**
 * Library for creating web applications
 * 
 * @author Tony
 *
 */
public class WebLeolaLibrary implements LeolaLibrary {

    private Leola runtime;

    /* (non-Javadoc)
     * @see leola.vm.lib.LeolaLibrary#init(leola.vm.Leola, leola.vm.types.LeoNamespace)
     */
    @Override
    public void init(Leola leola, LeoNamespace namespace) throws LeolaRuntimeException {
        this.runtime = leola;
        leola.putIntoNamespace(this, namespace);

    }

    /**
     * constructs a new {@link WebApp} that contains an embedded web server that allows
     * routes to be bound to {@link LeoObject} functions.
     * 
     * @return the {@link WebApp}
     */
    public WebApp newWebApp(LeoMap config) {
        WebApp app = new WebApp(this.runtime, config);
        return app;
    }

    public WebResponse ok() {
        return new WebResponse(HttpStatus.OK);
    }

    public WebResponse noContent() {
        return new WebResponse(HttpStatus.NO_CONTENT);
    }

    public WebResponse created() {
        return new WebResponse(HttpStatus.CREATED);
    }

    public WebResponse accepted() {
        return new WebResponse(HttpStatus.ACCEPTED);
    }

    public WebResponse notModified() {
        return new WebResponse(HttpStatus.NOT_MODIFIED);
    }

    public WebResponse notFound() {
        return new WebResponse(HttpStatus.NOT_FOUND);
    }

    public WebResponse notAcceptable() {
        return new WebResponse(HttpStatus.NOT_ACCEPTABLE);
    }

    public WebResponse unauthorized() {
        return new WebResponse(HttpStatus.UNAUTHORIZED);
    }

    public WebResponse badRequest() {
        return new WebResponse(HttpStatus.BAD_REQUEST);
    }

    public WebResponse forbidden() {
        return new WebResponse(HttpStatus.FORBIDDEN);
    }

    public WebResponse serverError() {
        return new WebResponse(HttpStatus.INTERNAL_SERVER_ERROR);
    }

    /**
     * Issue a redirect
     * 
     * @param url
     * @return the {@link WebResponse} configured for a redirect
     */
    public WebResponse redirect(String url) {
        return new WebResponse(HttpStatus.MOVED_PERMANENTLY).redirect(url);
    }

    /**
     * Constructs a {@link WebResponse} with the supplied status HTTP code.
     * 
     * @param status the HTTP status code
     * @return the {@link WebResponse}
     */
    public WebResponse status(int status) {
        return new WebResponse(status);
    }

    /**
     * Converts the supplied {@link LeoObject} into a JSON {@link String}
     * 
     * @param obj
     * @return the JSON string
     */
    public static String toJson(LeoObject obj) {
        Gson gson = new GsonBuilder().create();
        return gson.toJson(obj);
    }

    /**
     * Converts the supplied JSON {@link String} into a {@link LeoObject}
     * 
     * @param message
     * @return the {@link LeoObject}
     */
    @LeolaMethod(alias = "fromJson")
    public static LeoObject fromJson(String message) {
        Gson gson = new GsonBuilder().create();
        JsonElement element = gson.fromJson(message, JsonElement.class);
        return toLeoObject(element);
    }

    @LeolaIgnore
    public static LeoObject fromJson(InputStream iStream) throws IOException {
        Gson gson = new GsonBuilder().create();
        JsonElement element = gson.fromJson(new InputStreamReader(iStream), JsonElement.class);
        return toLeoObject(element);
    }

    /**
     * Converts the {@link JsonElement} into the equivalent {@link LeoObject}
     * 
     * @param element
     * @return the {@link LeoObject}
     */
    private static LeoObject toLeoObject(JsonElement element) {
        if (element == null || element.isJsonNull()) {
            return LeoObject.NULL;
        }

        if (element.isJsonArray()) {
            JsonArray array = element.getAsJsonArray();
            LeoArray leoArray = new LeoArray(array.size());
            array.forEach(e -> leoArray.add(toLeoObject(e)));
            return leoArray;
        }

        if (element.isJsonObject()) {
            JsonObject object = element.getAsJsonObject();
            LeoMap leoMap = new LeoMap();
            object.entrySet().forEach(entry -> {
                leoMap.putByString(entry.getKey(), toLeoObject(entry.getValue()));
            });

            return leoMap;
        }

        if (element.isJsonPrimitive()) {
            JsonPrimitive primitive = element.getAsJsonPrimitive();
            if (primitive.isBoolean()) {
                return LeoObject.valueOf(primitive.getAsBoolean());
            }

            if (primitive.isNumber()) {
                return LeoObject.valueOf(primitive.getAsDouble());
            }

            if (primitive.isString()) {
                return LeoString.valueOf(primitive.getAsString());
            }
        }

        return LeoObject.NULL;
    }

    /**
     * @return the host name of the server this application is running on
     */
    public String hostname() {
        try {
            return InetAddress.getLocalHost().getHostName();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}