Android Open Source - rcdroid Asset Resource Handler






From Project

Back to project page rcdroid.

License

The source code is released under:

Copyright (c) 2013, Jamie Furness (jamie@jamierf.co.uk) All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following...

If you think the Android project rcdroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.jamierf.rcdroid.http.handler;
/*w w w .j  a v a2s  .  c o m*/
import android.content.res.AssetManager;
import com.google.common.collect.ImmutableList;
import org.webbitserver.HttpControl;
import org.webbitserver.HttpRequest;
import org.webbitserver.HttpResponse;
import org.webbitserver.handler.AbstractResourceHandler;
import org.webbitserver.handler.FileEntry;
import org.webbitserver.handler.StaticFile;
import org.webbitserver.handler.TemplateEngine;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.Executor;

import static java.util.concurrent.Executors.newFixedThreadPool;

public class AssetResourceHandler extends AbstractResourceHandler {

    private final AssetManager assets;

    public AssetResourceHandler(AssetManager assets, Executor ioThread, TemplateEngine templateEngine) {
        super(ioThread, templateEngine);

        this.assets = assets;
    }

    public AssetResourceHandler(AssetManager assets, Executor ioThread) {
        this(assets, ioThread, new StaticFile());
    }

    public AssetResourceHandler(AssetManager assets, TemplateEngine templateEngine) {
        this(assets, newFixedThreadPool(4), templateEngine);
    }

    public AssetResourceHandler(AssetManager assets) {
        this(assets, newFixedThreadPool(4));
    }

    @Override
    protected ResourceWorker createIOWorker(HttpRequest request, HttpResponse response, HttpControl control) {
        return new ResourceWorker(request, response, control);
    }

    protected class ResourceWorker extends AbstractResourceHandler.IOWorker {
        private InputStream resource;
        private final String pathWithoutTrailingSlash;
        private final boolean isDirectory;

        protected ResourceWorker(HttpRequest request, HttpResponse response, HttpControl control) {
            super(request.uri(), request, response, control);
            isDirectory = path.endsWith("/");
            pathWithoutTrailingSlash = withoutQuery(isDirectory ? path.substring(0, path.length() - 1) : path);
        }

        @Override
        protected boolean exists() throws IOException {
            resource = getAsset(pathWithoutTrailingSlash);
            return resource != null;
        }

        @Override
        protected boolean isDirectory() throws IOException {
            return isDirectory;
        }

        @Override
        protected byte[] fileBytes() throws IOException {
            if (resource == null || isDirectory()) {
                return null;
            } else {
                return read(resource);
            }
        }

        @Override
        protected byte[] welcomeBytes() throws IOException {
            InputStream resourceStream = getAsset(welcomeFileName);
            return resourceStream == null ? null : read(resourceStream);
        }

        @Override
        protected byte[] directoryListingBytes() throws IOException {
            final ImmutableList.Builder<FileEntry> entries = ImmutableList.builder();

            final String[] files = assets.list("./");
            for (String file : files)
                entries.add(new FileEntry(file));

            return isDirectory() ? directoryListingFormatter.formatFileListAsHtml(entries.build()) : null;
        }

        private byte[] read(InputStream content) throws IOException {
            try {
                return read(content.available(), content);
            } catch (NullPointerException happensWhenReadingDirectoryPathInJar) {
                return null;
            }
        }

        private InputStream getAsset(String assetPath) throws IOException {
            // remove the initial /
            if (assetPath.startsWith("/"))
                assetPath = assetPath.substring(1);

            try {
                return assets.open(assetPath);
            }
            catch (FileNotFoundException e) {
                return null;
            }
        }
    }
}




Java Source Code List

com.jamierf.rcdroid.CarActivity.java
com.jamierf.rcdroid.CarService.java
com.jamierf.rcdroid.config.CarConfig.java
com.jamierf.rcdroid.config.ServoConfig.java
com.jamierf.rcdroid.config.WebUIConfig.java
com.jamierf.rcdroid.http.Packet.java
com.jamierf.rcdroid.http.WebController.java
com.jamierf.rcdroid.http.handler.AssetResourceHandler.java
com.jamierf.rcdroid.http.handler.ControlProtocolHandler.java
com.jamierf.rcdroid.http.handler.JsonPOJOHandler.java
com.jamierf.rcdroid.http.listener.ClientListener.java
com.jamierf.rcdroid.http.listener.PacketListener.java
com.jamierf.rcdroid.input.SensorController.java
com.jamierf.rcdroid.input.api.BatteryStatus.java
com.jamierf.rcdroid.input.api.Coordinate.java
com.jamierf.rcdroid.input.sensor.AbstractSensor.java
com.jamierf.rcdroid.input.sensor.AccelerationSensor.java
com.jamierf.rcdroid.input.sensor.BatterySensor.java
com.jamierf.rcdroid.input.sensor.LocationSensor.java
com.jamierf.rcdroid.input.sensor.RotationSensor.java
com.jamierf.rcdroid.input.sensor.listener.SensorListener.java
com.jamierf.rcdroid.logic.CarEngine.java
com.jamierf.rcdroid.logic.packets.AbstractServoHandler.java
com.jamierf.rcdroid.logic.packets.PlayAlarmRequestHandler.java
com.jamierf.rcdroid.logic.packets.SetRotationRequestHandler.java
com.jamierf.rcdroid.logic.packets.SetSpeedRequestHandler.java
com.jamierf.rcdroid.logic.sensors.AccelerationSensorHandler.java
com.jamierf.rcdroid.logic.sensors.BatterySensorHandler.java
com.jamierf.rcdroid.logic.sensors.LocationSensorHandler.java
com.jamierf.rcdroid.logic.sensors.RotationSensorHandler.java