Example #1
0
  public static String getCrumbRequestField() {
    CrumbIssuer issuer = Hudson.getInstance().getCrumbIssuer();
    if (issuer != null) {
      return issuer.getDescriptor().getCrumbRequestField();
    }

    return "";
  }
  /*
   * Path: /auto/provider
   *
   * Generate the service provider description document.
   */
  public void doProvider(StaplerRequest request, StaplerResponse response)
      throws URISyntaxException, WebApplicationException, IOException {
    requireGET();

    ServiceProvider provider = new ServiceProvider();
    provider.setAbout(getProviderURI());
    provider.setTitle("OSLC Automation Provider for Hudson and Jenkins");
    provider.setPublisher(new Publisher("Eclipse Lyo", "urn:oslc:ServiceProvider"));

    final PrefixDefinition[] prefixDefinitions = getPrefixDefinitions();
    provider.setPrefixDefinitions(prefixDefinitions);

    Service service = new Service();
    provider.addService(service);
    service.setDomain(new URI(AutomationConstants.AUTOMATION_DOMAIN));
    service.setUsages(new URI[] {new URI(AutomationConstants.AUTOMATION_NAMESPACE + "Build")});

    QueryCapability queryJobs =
        new QueryCapability("Jobs", getBaseUriBuilder().path("queryJobs").build());
    queryJobs.addResourceType(new URI(AutomationConstants.TYPE_AUTOMATION_PLAN));
    service.addQueryCapability(queryJobs);

    QueryCapability queryRuns =
        new QueryCapability("Runs", getBaseUriBuilder().path("queryRuns").build());
    queryRuns.addResourceType(new URI(AutomationConstants.TYPE_AUTOMATION_RESULT));
    service.addQueryCapability(queryRuns);

    UriBuilder creationFactoryUriBuilder = getBaseUriBuilder().path("scheduleBuild");

    /*
     * Hudson uses crumbs to prevent CSRF attacks on POST requests. OSLC
     * interfaces cannot support this, however. To workaround -- for now at
     * least -- bake the crumb into the URL. Then we can use OAuth or
     * another authentication mechanism to avoid CSRF problems.
     */
    CrumbIssuer issuer = Hudson.getInstance().getCrumbIssuer();
    if (issuer != null) {
      String crumbName = issuer.getDescriptor().getCrumbRequestField();
      String crumb = issuer.getCrumb(null);
      creationFactoryUriBuilder.queryParam(crumbName, crumb);
    }

    CreationFactory scheduleBuild =
        new CreationFactory("Schedule Build", creationFactoryUriBuilder.build());
    scheduleBuild.addResourceType(new URI(AutomationConstants.TYPE_AUTOMATION_REQUEST));
    service.addCreationFactory(scheduleBuild);

    Dialog selectJobs = new Dialog("Select Job", getBaseUriBuilder().path("selectJob").build());
    selectJobs.addResourceType(new URI(AutomationConstants.TYPE_AUTOMATION_PLAN));
    selectJobs.setHintHeight("400px");
    selectJobs.setHintWidth("600px");
    service.addSelectionDialog(selectJobs);

    marshal(provider);
  }