Adding a REST route endpoint


#1

In my MyPluginModule.java I add addResource(MyPluginResource.class);

My class looks like this

@Api(value = "Logo", description = "Upload New Logo")
@Path("/uploadLogo")
public class MyPluginResource implements PluginRestResource {

    @PUT
    @Timed
    @ApiOperation(value = "Upload Logo")
    @Consumes(MediaType.APPLICATION_JSON)
    public void String request) {
       //process JSON request string here
    }
}

From my React frontend I create a ajax method with the route /plugins/org.graylog.plugins/uploadLogo


(Jochen) #2

What’s your question?


#3

How do I properly set up a REST endpoint for passing data back!? I followed the example in the Map plugin on the graylog github. I thought I had everything setup correctly (see first post). But when I try to do a PUT it says that the path does not exist. So I must be missing something.


(Jochen) #4

You’re not using the correct URI to access your JAX-RS resource.

The plugin resources are prefixed by the fully qualified package name of the class implementing Plugin, e. g. org.graylog.plugins.map in case of the Map Widget plugin.


What will be full web services url for graylog plugin
#5

I ensured that my URI is referencing my package name. But it still does not work.

My folder structure is this

I ensured that my URI in the index.js is pointing to /plugins/org.graylog.plugins.logo/uploadLogo


(Jochen) #6

Please share the relevant code of your Java and JavaScript components.


#7

LogoReplacerConfig.jsx

  $.ajax({
      type: 'POST',
      url: '/plugins/org.graylog.plugins.logo/uploadLogo',
      data: this.state.config,
      processData: false,
      contentType: false
    })
    .done(function(data) {
      console.log('success');
      this._closeModal();
    });

LogoReplacerResource.java

package org.graylog.plugins.logo.rest;
@RequiresAuthentication
@Api(value = "Logo", description = "Upload New Logo")
@Path("/uploadLogo")
public class LogoReplacerResource implements PluginRestResource {
    private static final Logger LOG = LoggerFactory.getLogger(LogoReplacerResource.class);

    @POST
    @Timed
    @ApiOperation(value = "Upload Logo")
    @Consumes(MediaType.APPLICATION_JSON)
    @NoAuditEvent("only used to uplooad a new image to graylog")
    public void upload(@ApiParam(name = "image", required = true) String request) {
    }
}

LogoReplacerModule.java

package org.graylog.plugins.logo;
import org.graylog2.plugin.PluginConfigBean;
import org.graylog2.plugin.PluginModule;
import org.graylog.plugins.logo.rest.LogoReplacerResource;
public class LogoReplacerModule extends PluginModule {
    private static final Logger LOG = LoggerFactory.getLogger(LogoReplacerModule.class);
  
    @Override
    public Set<? extends PluginConfigBean> getConfigBeans() {
        return Collections.emptySet();
    }

    @Override
    protected void configure() {
      addRestResource(LogoReplacerResource.class);
    }
}

(Jochen) #8

You’re not sending any authentication information to the resource but annotated it with @RequiresAuthentication.
Additionally, you’re not sending any content type which isn’t strictly required but recommended.


#9

Adding a content type and removing @RequiresAuthentication still resolves in a POST http://localhost:8080/plugins/org.graylog.plugins.logo/uploadLogo 404 (Not Found)

$.ajax({
      type: 'POST',
      url: '/plugins/org.graylog.plugins.logo/uploadLogo',
      data: 'text',
      contentType: 'plain/text'
    })
    .done(function(data) {
      console.log('success');
      this._closeModal();
    });

(Jochen) #10

Did you properly register your resource?
Did you try using the correct content type? It says application/json in the JAX-RS resource but plain/text (did you mean text/plain?) in the JavaScript code.

Anyway, I will stop guessing here without having access to the full code.


#11

I have registered my resource in the module by using addRestResource(LogoReplacerResource.class). Here is a link to my source code graylog-plugin-logo-replacer


(Jochen) #12

When building the plugin from the GitHub repository you’ve posted and installing it in Graylog, I get the following output:

$ curl -u admin:admin -i -X POST -H 'Content-Type: application/json' -d '{}' http://127.0.0.1:9000/api/plugins/org.graylog.plugins.logo/uploadLogo
HTTP/1.1 204 No Content
X-Graylog-Node-ID: cd03ee44-b2a7-4824-be16-bb7456149dbd
X-Runtime-Microseconds: 724
Date: Fri, 28 Jul 2017 11:49:20 GMT

This means that the resource has been properly registered and is accessible through the Graylog REST API. It’s also shown in the API browser.


#13

Awesome!!! So I was using the wrong URL to post to in my frontend component. I was hitting the standard web interface (port 8080) and not the API route (port 9000). Thanks so much for your help!


#14

Also from my react component I needed to to a fetch call instead of a jQuery ajax call.


(system) #15

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.