Android Open Source - Cardeto Cardeto Web Server






From Project

Back to project page Cardeto.

License

The source code is released under:

Apache License

If you think the Android project Cardeto 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.ggt.cardeto.embeddedwebserver;
//from w w  w.j  a  va2  s . com
import android.content.Context;
import android.os.Looper;
import android.text.TextUtils;

import com.ggt.cardeto.R;
import com.ggt.cardeto.externallib.NanoHTTPD;
import com.ggt.cardeto.utils.CardetoConstants;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;

/**
 * Nano http server of cardeto. Handles module management and dispatch.
 *
 * @author guiguito
 */
public class CardetoWebServer extends NanoHTTPD {

    private Context mContext;

    private ArrayList<CardetoWebServerModule> mModules = new ArrayList<CardetoWebServerModule>();

    public CardetoWebServer(Context context, int port) throws IOException {
        super(port, null);
        mContext = context;
    }

    public void addCardetoWebServerModule(
            CardetoWebServerModule cardetoWebServerModule) {
        mModules.add(cardetoWebServerModule);
    }

    public void removeCardetoWebServerModule(
            CardetoWebServerModule cardetoWebServerModule) {
        mModules.remove(cardetoWebServerModule);
    }

    public Response serve(String uri, String method, Properties header,
                          Properties parms, Properties files) {
        Looper.prepare();
        boolean isHandled = false;
        StringBuilder responsePage = new StringBuilder();
        for (CardetoWebServerModule cardetoWebServerModule : mModules) {
            if (cardetoWebServerModule.matchURI(uri)) {
                responsePage = cardetoWebServerModule.handleRequest(uri,
                        method, header, parms, files);
                isHandled = true;
                break;
            }
        }
        if (!isHandled) {
            // no module matched then display welcome page
            responsePage.append(mContext.getString(R.string.html_header));
            responsePage.append(TextUtils.htmlEncode(mContext
                    .getString(R.string.no_module_module))
                    + CardetoConstants.HTML_RETURN);
            if (mModules.size() > 0) {
                responsePage.append("<table>\n<tbody>\n");

                responsePage.append("<tr>\n");

                responsePage.append("<td>\n");
                responsePage.append(TextUtils.htmlEncode(mContext
                        .getString(R.string.module)) + "\n");
                responsePage.append("</td>\n");

                responsePage.append("<td>\n");
                responsePage.append(TextUtils.htmlEncode(mContext
                        .getString(R.string.description)) + "\n");
                responsePage.append("</td>\n");

                responsePage.append("<td>\n");
                responsePage.append(TextUtils.htmlEncode(mContext
                        .getString(R.string.url)) + "\n");
                responsePage.append("</td>\n");

                responsePage.append("</tr>\n");
                for (CardetoWebServerModule cardetoWebServerModule : mModules) {

                    responsePage.append("<tr>\n");

                    responsePage.append("<td>\n");
                    responsePage
                            .append(TextUtils.htmlEncode(cardetoWebServerModule
                                    .getModuleTitle()) + "\n");
                    responsePage.append("</td>\n");

                    responsePage.append("<td>\n");
                    responsePage
                            .append(TextUtils.htmlEncode(cardetoWebServerModule
                                    .getDescription()) + "\n");
                    responsePage.append("</td>\n");

                    responsePage.append("<td>\n");
                    responsePage.append("<a href=\""
                            + cardetoWebServerModule.getUrl() + "\">"
                            + cardetoWebServerModule.getUrl() + "</a>"
                            + CardetoConstants.HTML_RETURN + "\n");
                    responsePage.append("</td>\n");

                    responsePage.append("</tr>\n");
                }
                responsePage.append("</tbody>\n</table>\n");
            }
            responsePage.append(mContext.getString(R.string.html_footer));
        }

        return new NanoHTTPD.Response(NanoHTTPD.HTTP_OK, MIME_HTML,
                responsePage.toString());
    }
}




Java Source Code List

com.ggt.cardeto.CardetoService.java
com.ggt.cardeto.embeddedwebserver.CardetoWebServerModule.java
com.ggt.cardeto.embeddedwebserver.CardetoWebServer.java
com.ggt.cardeto.embeddedwebserver.appinfomodule.AppInfoModule.java
com.ggt.cardeto.embeddedwebserver.clipboardmodule.ClipboardModule.java
com.ggt.cardeto.embeddedwebserver.logcatmodule.LogcatModule.java
com.ggt.cardeto.embeddedwebserver.sqlitemodule.GenericSQLiteDatabaseHelper.java
com.ggt.cardeto.embeddedwebserver.sqlitemodule.SQLiteModule.java
com.ggt.cardeto.embeddedwebserver.sqlitemodule.tableouputrenderers.CSVRenderer.java
com.ggt.cardeto.embeddedwebserver.sqlitemodule.tableouputrenderers.HtmlRenderer.java
com.ggt.cardeto.embeddedwebserver.sqlitemodule.tableouputrenderers.JsonRenderer.java
com.ggt.cardeto.embeddedwebserver.sqlitemodule.tableouputrenderers.TableOutputRenderer.java
com.ggt.cardeto.embeddedwebserver.sqlitemodule.tableouputrenderers.XmlRenderer.java
com.ggt.cardeto.embeddedwebserver.staticvarsmodule.StaticVarsModule.java
com.ggt.cardeto.externallib.NanoHTTPD.java
com.ggt.cardeto.utils.CardetoConstants.java
com.ggt.cardeto.utils.NetworkUtilsOld.java
com.ggt.cardeto.utils.NetworkUtils.java
com.ggt.cardetosample.TestActivity.java
com.ggt.cardetosample.database.CardetoSampleDatabaseDataSource.java
com.ggt.cardetosample.database.CardetoSampleDatabaseHelper.java
com.ggt.cardetosample.database.Log.java