扩展Nanohttp实现以处理自定义HTTP方法

时间:2018-01-31 12:39:11

标签: java cordova nanohttpd

我一直在尝试使用this cordova plugin,它使用NanoHttpd来处理请求 默认情况下,Nanohttpd处理some of the HTTP methods,如GET,POST,CONNECT,PROPFIND,PATCH等。

我一直在试图弄清楚如何实现自定义处理程序,以便nanohttpd可以处理更多的HTTP方法,例如:NOTIFY和SUBSCRIBE

@Override
    public Response serve(IHTTPSession session) {
        Log.d(this.getClass().getName(), "New request is incoming!");

        String requestUUID = UUID.randomUUID().toString();

        PluginResult pluginResult = null;
        try {
            pluginResult = new PluginResult(
                    PluginResult.Status.OK, this.createJSONRequest(requestUUID, session));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        pluginResult.setKeepCallback(true);
        this.webserver.onRequestCallbackContext.sendPluginResult(pluginResult);

        while (!this.webserver.responses.containsKey(requestUUID)) {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        JSONObject responseObject = (JSONObject) this.webserver.responses.get(requestUUID);
        Log.d(this.getClass().getName(), "responseObject: " + responseObject.toString());
        Response response = null;

        try {
            response = newFixedLengthResponse(
                    Response.Status.lookup(responseObject.getInt("status")),
                    getContentType(responseObject),
                    responseObject.getString("body")
            );

            Iterator<?> keys = responseObject.getJSONObject("headers").keys();
            while (keys.hasNext()) {
                String key = (String) keys.next();
                response.addHeader(
                        key,
                        responseObject.getJSONObject("headers").getString(key)
                );
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return response;
}

我添加了一个简单的notify响应来处理任何传入的请求,请参考此处 - https://stackoverflow.com/a/27645191/2096740

public Response notify(IHTTPSession session) {
        StringBuilder text = new StringBuilder("<html><body>");
        text.append("<h1>Url: ");
        text.append(session.getUri());
        text.append("</h1><br>");
        Map<String, String> queryParams = session.getParms();
        if (queryParams.size() > 0) {
            for (Map.Entry<String, String> entry : queryParams.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                text.append("<p>Param '");
                text.append(key);
                text.append("' = ");
                text.append(value);
                text.append("</p>");
            }
        } else {
            text.append("<p>no params in url</p><br>");
        }
        return newFixedLengthResponse(text.toString());
    }

但这会返回BAD REQUEST: Syntax error. HTTP verb NOTIFY unhandled.

文档不明确,关于在SO上或通过网络结果扩展Nanohttpd行为的信息不多。

这样做的正确方法是什么?我该如何扩展呢?

1 个答案:

答案 0 :(得分:0)

Method的检查实际上已锁定在枚举中。它是hardcoded,没有其他方法可以扩展。

getMethod实例本身是一种枚举类型的方法。

因为,我找不到任何其他解决方案,因此我得出结论,在Nanohttpd中不可能做到这一点。 Maven中的所有版本都不支持此功能。

他们有

的原因
  

对HEAD,POST和DELETE请求的一些内置支持。您可以   但是,可以轻松实现/自定义任何HTTP方法。

其功能列表中提到的

是因为original version将方法作为String。自那以后它发生了变化。

功能列表尚未更新以反映此更改。