webmethods - Webmethods IntegrationServer http 响应和请求的拦截器

标签 webmethods

我如何在 Integration Server 中为 http-503 请求服务不可用错误设置响应 header 。由于它没有命中我的服务,我无法使用 pub.flow:setResponseHeader 设置标题。有什么方法可以让拦截器设置响应 header 吗?

最佳答案

不幸的是,没有用于拦截对 Integration Server 的 HTTP 请求的公共(public) API。

但是您可以使用私有(private) API 来做您想做的事情,即返回 HTTP 503 Service Unavailable 响应到 HTTP GETPOST 当请求的服务不存在时向 /invoke URL 请求。

下面是一个示例类,它实现了 com.wm.app.b2b.server.HTTPHandler 接口(interface),并处理对 /invoke 指令的 HTTP 请求。如果所请求的服务存在,HTTP 请求将被传递到内置的 Integration Server 调用处理程序 com.wm.app.b2b.server.HTTPInvokeHandler 进行处理。如果不存在,则会将您选择的 HTTP 响应代码和消息返回给 HTTP 客户端。

import com.wm.app.b2b.server.AccessException;
import com.wm.app.b2b.server.BaseService;
import com.wm.app.b2b.server.HTTPDispatch;
import com.wm.app.b2b.server.HTTPHandler;
import com.wm.app.b2b.server.HTTPInvokeHandler;
import com.wm.app.b2b.server.ProtocolState;
import com.wm.app.b2b.server.ServerAPI;
import com.wm.app.b2b.server.ns.Namespace;
import com.wm.lang.ns.NSName;
import com.wm.util.Config;

import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * webMethods Integration Server HTTP invoke handler which returns a
 * custom HTTP response code and message when the invoked service does
 * not exist.
 */
public class MissingServiceResponseHandler implements HTTPHandler {
    private static final String DEFAULT_INVOKE_DIRECTIVE = "invoke";
    private static final String INVOKE_DIRECTIVE_CONFIG_KEY = "watt.server.invokeDirective";

    private int responseCode;
    private String responseMessage;
    private Set<String> directives;
    private Map<String, HTTPHandler> originalHandlers;
    private HTTPInvokeHandler invokeHandler;
    private boolean registered;

    /**
     * Constructs a new MissingServiceResponseHandler that returns the specified
     * HTTP response code and message when the invoked service does not exist.
     * @param responseCode      The HTTP response code returned when the invoked
     *                          service does not exist.
     * @param responseMessage   The HTTP response message returned when the invoked
     *                          service does not exist.
     */
    public MissingServiceResponseHandler(int responseCode, String responseMessage) {
        this.responseCode = responseCode;
        this.responseMessage = responseMessage;
        this.directives = getDirectives();
        this.originalHandlers = new TreeMap<String, HTTPHandler>();
        this.invokeHandler = new HTTPInvokeHandler();
        this.registered = false;
    }

    /**
     * Processes an HTTP request; called by the webMethods Integration Server HTTP request dispatcher.
     * @param  state           The HTTP request to be processed.
     * @return                 True if this object was able to process the HTTP request, otherwise false.
     * @throws IOException     If an I/O problem is encountered reading from or writing to the client socket.
     * @throws AccessException If the the HTTP request requires authentication or is not authorized.
     */
    @Override
    public final boolean process(ProtocolState state) throws IOException, AccessException {
        boolean result;

        String directive = getDirective(state);
        String path = state.getHttpRequestUrl().substring(directive.length());
        StringTokenizer tokenizer = new StringTokenizer(path, "/:");
        NSName serviceName = null;
        BaseService service = null;

        if (tokenizer.countTokens() > 1) {
            serviceName = NSName.create(tokenizer.nextToken(), tokenizer.nextToken());
            service = Namespace.getService(serviceName);
        }

        if (serviceName == null || service == null) {
            // service does not exist, to return custom response code and message
            state.setResponse(responseCode, responseMessage);
            result = true;
        } else {
            state.getInvokeState().setService(service);
            result = invokeHandler._process(state, ServerAPI.getContentHandler(state.getContentType()), serviceName);
        }

        return result;
    }

    /**
     * Returns the relevant directive that matches the given protocol state.
     * @param state The protocol state to resolve the directive against.
     * @return      The relevant directive that matches the given protocol state.
     */
    private String getDirective(ProtocolState state) {
        String directive = DEFAULT_INVOKE_DIRECTIVE;
        if (!state.getHttpRequestUrl().startsWith(directive)) {
            directive = Config.getProperty(DEFAULT_INVOKE_DIRECTIVE, INVOKE_DIRECTIVE_CONFIG_KEY);
        }
        return directive;
    }

    /**
     * Returns all the configured invoke directives for this Integration Server.
     * @return All the configured invoke directives for this Integration Server.
     */
    private static Set<String> getDirectives() {
        Set<String> directives = new TreeSet<String>();

        directives.add(DEFAULT_INVOKE_DIRECTIVE);

        String alternateDirective = Config.getProperty(DEFAULT_INVOKE_DIRECTIVE, INVOKE_DIRECTIVE_CONFIG_KEY);
        if (!DEFAULT_INVOKE_DIRECTIVE.equals(alternateDirective)) {
            directives.add(alternateDirective);
        }

        return directives;
    }

    /**
     * Registers this object as a handler for the invoke directives with the
     * Integration Server HTTP request dispatcher.
     */
    public synchronized void register() {
        if (!registered) {
            for (String directive : directives) {
                // save the original handler, so we can restore it later
                originalHandlers.put(directive, HTTPDispatch.getHandler(directive));
                // replace the original handler with this custom handler
                HTTPDispatch.addHandler(directive, this);
            }
            registered = true;
        }
    }

    /**
     * Unregisters this object as a handler of the invoke directives from the
     * Integration Server HTTP request dispatcher, and reinstates the original
     * handlers.
     */
    public synchronized void unregister() {
        if (registered) {
            for (String directive : directives) {
                // remove this custom handler from the dispatcher
                HTTPDispatch.removeHandler(directive);
                // then restore the original handler
                HTTPDispatch.addHandler(directive, originalHandlers.get(directive));
            }
            registered = false;
        }
    }
}

这个类的使用示例如下:

// create a new handler, specifying the desired HTTP response code and message
// returned when the requested service does not exist
MissingServiceResponseHandler handler = new MissingServiceResponseHandler(503, "Service Unavailable");

// register our custom handler with the Integration Server HTTP request dispatcher
handler.register();

// ...
// now all HTTP requests to the /invoke directive will be handled by our custom handler
// ...

// unregister our custom handler with the Integration Server HTTP request dispatcher, restoring the built-in invoke handler
handler.unregister();

// ...
// now all HTTP requests to the /invoke directive will revert to being handled by the built-in invoke handler
// ...

关于webmethods - Webmethods IntegrationServer http 响应和请求的拦截器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31310933/

相关文章:

scala - 将 Akka 流连接到 JMS

webmethods - 在 webMethods 中创建/安排调度程序以调用具有 2 个(或更多)输入选项的服务

c# - WEBMethods (Java) 未写入网络驱动器?

webmethods - 如何从 Webmethods 的文档列表中消除重复文档?

linux - 如何在 linux 上检查 Webmethod 版本?

integration - Web方法6.5调度程序在IS重新启动后丢失

unit-testing - 流服务中的 Webmethods 模拟

webmethods - 如何在 webMethods 流服务中将文档列表中的文档作为行添加到字符串表

web-services - 使用 HTTPS URL 在 webMethods 中创建 Web 服务描述符(消费者)