Search in sources :

Example 1 with TaskForm

use of org.pdown.rest.form.TaskForm in project proxyee-down by monkeyWie.

the class HttpDownAppCallback method commonHook.

private void commonHook(HttpDownBootstrap httpDownBootstrap, String event, boolean async) {
    DownInfo downInfo = findDownInfo(httpDownBootstrap);
    Map<String, Object> taskInfo = buildTaskInfo(downInfo);
    if (taskInfo != null) {
        // 遍历扩展模块是否有对应的处理
        List<ExtensionInfo> extensionInfos = ExtensionContent.get();
        for (ExtensionInfo extensionInfo : extensionInfos) {
            if (extensionInfo.getMeta().isEnabled()) {
                if (extensionInfo.getHookScript() != null && !StringUtils.isEmpty(extensionInfo.getHookScript().getScript())) {
                    Event e = extensionInfo.getHookScript().hasEvent(event, HttpDownUtil.getUrl(httpDownBootstrap.getRequest()));
                    if (e != null) {
                        try {
                            // 执行钩子函数
                            Object result = ExtensionUtil.invoke(extensionInfo, e, taskInfo, async);
                            if (result != null) {
                                ObjectMapper objectMapper = new ObjectMapper();
                                String temp = objectMapper.writeValueAsString(result);
                                TaskForm taskForm = objectMapper.readValue(temp, TaskForm.class);
                                if (taskForm.getRequest() != null) {
                                    httpDownBootstrap.setRequest(HttpDownUtil.buildRequest(taskForm.getRequest().getMethod(), taskForm.getRequest().getUrl(), taskForm.getRequest().getHeads(), taskForm.getRequest().getBody()));
                                }
                                if (taskForm.getResponse() != null) {
                                    httpDownBootstrap.setResponse(taskForm.getResponse());
                                }
                                if (taskForm.getData() != null) {
                                    downInfo.setData(taskForm.getData());
                                }
                                HttpDownContent.getInstance().save();
                            }
                        } catch (Exception ex) {
                            LOGGER.error("An hook exception occurred while " + event + "()", ex);
                        }
                    }
                }
            }
        }
    }
}
Also used : DownInfo(org.pdown.rest.entity.DownInfo) ExtensionInfo(org.pdown.gui.extension.ExtensionInfo) TaskForm(org.pdown.rest.form.TaskForm) Event(org.pdown.gui.extension.HookScript.Event) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with TaskForm

use of org.pdown.rest.form.TaskForm in project proxyee-down by monkeyWie.

the class NativeController method onResolve.

@RequestMapping("onResolve")
public FullHttpResponse onResolve(Channel channel, FullHttpRequest request) throws Exception {
    HttpRequestForm taskRequest = getJSONParams(request, HttpRequestForm.class);
    // 遍历扩展模块是否有对应的处理
    List<ExtensionInfo> extensionInfos = ExtensionContent.get();
    for (ExtensionInfo extensionInfo : extensionInfos) {
        if (extensionInfo.getMeta().isEnabled()) {
            if (extensionInfo.getHookScript() != null && !StringUtils.isEmpty(extensionInfo.getHookScript().getScript())) {
                Event event = extensionInfo.getHookScript().hasEvent(HookScript.EVENT_RESOLVE, taskRequest.getUrl());
                if (event != null) {
                    try {
                        // 执行resolve方法
                        Object result = ExtensionUtil.invoke(extensionInfo, event, taskRequest, false);
                        if (result != null && !(result instanceof Undefined)) {
                            ObjectMapper objectMapper = new ObjectMapper();
                            String temp = objectMapper.writeValueAsString(result);
                            TaskForm taskForm = objectMapper.readValue(temp, TaskForm.class);
                            // 有一个扩展解析成功的话直接返回
                            return HttpHandlerUtil.buildJson(taskForm, Include.NON_DEFAULT);
                        }
                    } catch (Exception e) {
                        LOGGER.error("An exception occurred while resolve()", e);
                    }
                }
            }
        }
    }
    return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
}
Also used : Undefined(jdk.nashorn.internal.runtime.Undefined) ExtensionInfo(org.pdown.gui.extension.ExtensionInfo) HttpRequestForm(org.pdown.rest.form.HttpRequestForm) TaskForm(org.pdown.rest.form.TaskForm) Event(org.pdown.gui.extension.HookScript.Event) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ExtensionInfo (org.pdown.gui.extension.ExtensionInfo)2 Event (org.pdown.gui.extension.HookScript.Event)2 TaskForm (org.pdown.rest.form.TaskForm)2 IOException (java.io.IOException)1 Undefined (jdk.nashorn.internal.runtime.Undefined)1 DownInfo (org.pdown.rest.entity.DownInfo)1 HttpRequestForm (org.pdown.rest.form.HttpRequestForm)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1