Android Open Source - FxExplorer Response






From Project

Back to project page FxExplorer.

License

The source code is released under:

Apache License

If you think the Android project FxExplorer 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 net.gescobar.httpserver;
//from w ww .j a v  a2s.  c  om
import android.text.TextUtils;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

/**
 * Exposes convenient methods to write the response back to the client.
 *
 * @author German Escobar
 */
public class Response {

    public enum HttpStatus {

        OK(200, "OK"),
        CREATED(201, "Created"),
        ACCEPTED(202, "Accepted"),
        PARTIAL_INFO(203, "Partial Info"),
        NO_CONTENT(204, "No Content"),
        MOVED(301, "Moved Permanently"),
        FOUND(302, "Found"),
        SEE_OTHER(303, "See Other"),
        NOT_MODIFIED(304, "Not Modified"),
        BAD_REQUEST(400, "Bad Request"),
        UNAUTHORIZED(401, "Unauthorized"),
        FORBIDDEN(403, "Forbidden"),
        NOT_FOUND(404, "Not Found"),
        CONFLICT(409, "Conflict"),
        INTERNAL_ERROR(500, "Internal Error"),
        NOT_IMPLEMENTED(501, "Not Implemented"),
        OVERLOADED(502, "Overloaded"),
        GATEWAY_TIMEOUT(503, "Gateway Timeout");

        private int code;

        private String reason;

        private HttpStatus(int code, String reason) {
            this.code = code;
            this.reason = reason;
        }

        public int getCode() {
            return code;
        }

        public String getReason() {
            return reason;
        }

    }

    private String responseBody;
    private HttpStatus status;
    private String contentType;
    private boolean finished;
    private OutputStream outputStream;
    private HashMap<String, String> headers;

    public Response(OutputStream outputStream) {
        this.outputStream = outputStream;
        this.headers = new HashMap<>();
    }

    public OutputStream getOutputStream() {
        return this.outputStream;
    }

    public void addHeader(String key, String value) {
        this.headers.put(key, value);
    }

    public void finish() throws IOException {
        if (!finished) {
            outputStream.flush();
            outputStream.close();
            this.finished = true;
        }
    }

    public boolean isFinished() {
        return finished;
    }

    public Response status(HttpStatus status) {
        this.status = status;
        return this;
    }

    public Response ok() {
        this.status = HttpStatus.OK;
        return this;
    }

    public Response notFound() {
        this.status = HttpStatus.NOT_FOUND;
        return this;
    }

    public Response contentType(String contentType) {
        this.contentType = contentType;
        return this;
    }

    public void writeHeader() throws IOException {
        outputStream.write(headString().getBytes(Charset.forName("UTF-8")));
    }

    public void writeBody() throws IOException {
        if (!TextUtils.isEmpty(responseBody)) {
            outputStream.write(responseBody.getBytes(Charset.forName("UTF-8")));
        }
    }

    public void setBody(String body) {
        this.responseBody = body;
    }

    public String headString() {
        String ret = "HTTP/1.1 " + status.getCode() + " " + status.getReason() + "\r\n";
        if (status.getCode() != 200 || TextUtils.isEmpty(contentType)) {
            ret += "Content-Type: text/html;charset=utf8\r\n";
        } else {
            ret += "Content-Type: " + contentType + "\r\n";
        }
        for (Map.Entry<String, String> entry : headers.entrySet()) {
            ret += entry.getKey() + ": " + entry.getValue() + "\r\n";
        }
        return ret + "\r\n";
    }
}




Java Source Code List

info.breezes.fx.downloader.ApplicationTest.java
info.breezes.fx.downloader.DlMainActivity.java
info.breezes.fx.editor.ApplicationTest.java
info.breezes.fx.editor.EditorMainActivity.java
info.breezes.fx.player.ApplicationTest.java
info.breezes.fx.player.MainActivity.java
info.breezes.fx.viewer.ApplicationTest.java
info.breezes.fx.viewer.BigImageView.java
info.breezes.fx.viewer.ImageUtility.java
info.breezes.fx.viewer.MainActivity.java
info.breezes.fxmanager.ApplicationTest.java
info.breezes.fxmanager.FxApplication.java
info.breezes.fxmanager.LocalFileSystemProvider.java
info.breezes.fxmanager.MainActivity.java
info.breezes.fxmanager.MediaFragment.java
info.breezes.fxmanager.MediaItemUtil.java
info.breezes.fxmanager.MediaProvider.java
info.breezes.fxmanager.MenuAdapter.java
info.breezes.fxmanager.MimeTypeMap.java
info.breezes.fxmanager.NetUtils.java
info.breezes.fxmanager.PackagesProvider.java
info.breezes.fxmanager.ScanResultActivity.java
info.breezes.fxmanager.ScannerActivity.java
info.breezes.fxmanager.SettingsActivity.java
info.breezes.fxmanager.ShellUtil.java
info.breezes.fxmanager.StorageTool.java
info.breezes.fxmanager.ThemeChooserActivity.java
info.breezes.fxmanager.android.app.QAlertDialog.java
info.breezes.fxmanager.countly.CountlyActivity.java
info.breezes.fxmanager.countly.CountlyEvent.java
info.breezes.fxmanager.countly.CountlyFragment.java
info.breezes.fxmanager.countly.CountlyUtils.java
info.breezes.fxmanager.dialog.ApkInfoDialog.java
info.breezes.fxmanager.dialog.FileInfoDialog.java
info.breezes.fxmanager.dialog.HashInfoDialog.java
info.breezes.fxmanager.model.DrawerMenu.java
info.breezes.fxmanager.model.MediaItem.java
info.breezes.fxmanager.qrcode.QrBitmapDecoder.java
info.breezes.fxmanager.service.FileService.java
net.gescobar.httpserver.Handler.java
net.gescobar.httpserver.HttpConnection.java
net.gescobar.httpserver.HttpServer.java
net.gescobar.httpserver.Request.java
net.gescobar.httpserver.Response.java