org.sonar.api.server.ws
Interface WebService

All Superinterfaces:
Extension, ServerComponent, ServerExtension

public interface WebService
extends ServerExtension

Defines a web service. Note that contrary to the deprecated Webservice the ws is fully implemented in Java and does not require any Ruby on Rails code.

The classes implementing this extension point must be declared in Plugin.getExtensions().

How to use

 public class HelloWs implements WebService {
   @Override
   public void define(Context context) {
     NewController controller = context.createController("api/hello");
     controller.setDescription("Web service example");

     // create the URL /api/hello/show
     controller.createAction("show")
       .setDescription("Entry point")
       .setHandler(new RequestHandler() {
         @Override
         public void handle(Request request, Response response) {
           // read request parameters and generates response output
           response.newJsonWriter()
             .prop("hello", request.mandatoryParam("key"))
             .close();
         }
      })
      .createParam("key").setDescription("Example key").setRequired(true);

    // important to apply changes
    controller.done();
   }
 }
 

How to test

 public class HelloWsTest {
   WebService ws = new HelloWs();

   @Test
   public void should_define_ws() throws Exception {
     // WsTester is available in the Maven artifact org.codehaus.sonar:sonar-testing-harness
     WsTester tester = new WsTester(ws);
     WebService.Controller controller = tester.controller("api/hello");
     assertThat(controller).isNotNull();
     assertThat(controller.path()).isEqualTo("api/hello");
     assertThat(controller.description()).isNotEmpty();
     assertThat(controller.actions()).hasSize(1);

     WebService.Action show = controller.action("show");
     assertThat(show).isNotNull();
     assertThat(show.key()).isEqualTo("show");
     assertThat(index.handler()).isNotNull();
   }
 }
 

Since:
4.2

Nested Class Summary
static class WebService.Action
           
static class WebService.Context
           
static class WebService.Controller
           
static class WebService.NewAction
           
static class WebService.NewController
           
static class WebService.NewParam
           
static class WebService.Param
           
 
Method Summary
 void define(WebService.Context context)
          Executed once at server startup.
 

Method Detail

define

void define(WebService.Context context)
Executed once at server startup.



Copyright © 2009–2014 SonarSource. All rights reserved.