Search in sources :

Example 1 with DefaultGoPluginApiRequest

use of com.thoughtworks.go.plugin.api.request.DefaultGoPluginApiRequest in project gocd by gocd.

the class PluginRequestHelper method submitRequest.

public <T> T submitRequest(String pluginId, String requestName, PluginInteractionCallback<T> pluginInteractionCallback) {
    if (!pluginManager.isPluginOfType(extensionName, pluginId)) {
        throw new PluginNotFoundException(format("Did not find '%s' plugin with id '%s'. Looks like plugin is missing", extensionName, pluginId));
    }
    try {
        String resolvedExtensionVersion = pluginManager.resolveExtensionVersion(pluginId, goSupportedVersions);
        DefaultGoPluginApiRequest apiRequest = new DefaultGoPluginApiRequest(extensionName, resolvedExtensionVersion, requestName);
        apiRequest.setRequestBody(pluginInteractionCallback.requestBody(resolvedExtensionVersion));
        apiRequest.setRequestParams(pluginInteractionCallback.requestParams(resolvedExtensionVersion));
        GoPluginApiResponse response = pluginManager.submitTo(pluginId, apiRequest);
        if (response == null) {
            throw new RuntimeException("The plugin sent a null response");
        }
        if (DefaultGoApiResponse.SUCCESS_RESPONSE_CODE == response.responseCode()) {
            return pluginInteractionCallback.onSuccess(response.responseBody(), resolvedExtensionVersion);
        }
        throw new RuntimeException(format("The plugin sent a response that could not be understood by Go. Plugin returned with code '%s' and the following response: '%s'", response.responseCode(), response.responseBody()));
    } catch (Exception e) {
        throw new RuntimeException(format("Interaction with plugin with id '%s' implementing '%s' extension failed while requesting for '%s'. Reason: [%s]", pluginId, extensionName, requestName, e.getMessage()), e);
    }
}
Also used : DefaultGoPluginApiRequest(com.thoughtworks.go.plugin.api.request.DefaultGoPluginApiRequest) GoPluginApiResponse(com.thoughtworks.go.plugin.api.response.GoPluginApiResponse)

Example 2 with DefaultGoPluginApiRequest

use of com.thoughtworks.go.plugin.api.request.DefaultGoPluginApiRequest in project gocd by gocd.

the class PluginController method handlePluginInteractRequest.

@RequestMapping(value = "/plugin/interact/{pluginId}/{requestName}", method = { RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE })
public void handlePluginInteractRequest(@PathVariable String pluginId, @PathVariable String requestName, HttpServletRequest request, HttpServletResponse response) throws IOException {
    if (!isAuthPlugin(pluginId)) {
        response.setStatus(SC_FORBIDDEN);
        response.getWriter().println("Plugin interact endpoint is enabled only for Authentication Plugins");
        return;
    }
    if (isRestrictedRequestName(requestName)) {
        response.setStatus(SC_FORBIDDEN);
        response.getWriter().println(String.format("Plugin interact for '%s' requestName is disallowed.", requestName));
        return;
    }
    DefaultGoPluginApiRequest apiRequest = new DefaultGoPluginApiRequest(null, null, requestName);
    apiRequest.setRequestParams(getParameterMap(request));
    addRequestHeaders(request, apiRequest);
    try {
        GoPluginApiResponse pluginApiResponse = pluginManager.submitTo(pluginId, apiRequest);
        if (DefaultGoApiResponse.SUCCESS_RESPONSE_CODE == pluginApiResponse.responseCode()) {
            renderPluginResponse(pluginApiResponse, response);
            return;
        }
        if (DefaultGoApiResponse.REDIRECT_RESPONSE_CODE == pluginApiResponse.responseCode()) {
            String location = "";
            if (hasValueFor(pluginApiResponse, "Location")) {
                location = pluginApiResponse.responseHeaders().get("Location");
            }
            response.sendRedirect(location);
            return;
        }
    } catch (Exception e) {
    // handle
    }
    throw new RuntimeException("render error page");
}
Also used : DefaultGoPluginApiRequest(com.thoughtworks.go.plugin.api.request.DefaultGoPluginApiRequest) GoPluginApiResponse(com.thoughtworks.go.plugin.api.response.GoPluginApiResponse) IOException(java.io.IOException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

DefaultGoPluginApiRequest (com.thoughtworks.go.plugin.api.request.DefaultGoPluginApiRequest)2 GoPluginApiResponse (com.thoughtworks.go.plugin.api.response.GoPluginApiResponse)2 IOException (java.io.IOException)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1