<commit_before>package oasis.web.eventbus;

import java.net.URI;

import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.InvocationCallback;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Strings;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

import oasis.model.InvalidVersionException;
import oasis.model.applications.ApplicationRepository;
import oasis.model.applications.Subscription;
import oasis.model.applications.SubscriptionRepository;
import oasis.services.etag.EtagService;
import oasis.web.utils.ResponseFactory;
import oasis.web.authn.Authenticated;
import oasis.web.authn.Client;

@Authenticated
@Client
@Path("/e")
@Api(value = "/e", description = "EventBus API")
public class EventBusEndpoint {
  private static final Logger logger = LoggerFactory.getLogger(EventBusEndpoint.class);
  private static final String SECRET_HEADER = "X-Webhook-Secret";

  @Inject SubscriptionRepository subscriptionRepository;
  @Inject ApplicationRepository applicationRepository;
  @Inject EtagService etagService;

  @POST
  @Path("/publish")
  @Consumes(MediaType.APPLICATION_JSON)
  @ApiOperation(value = "Publish a typed event into the event bus.",
      notes = "The header '" + SECRET_HEADER + "' contains the secret defined by the application for this subscription." +
          "This will be replaced by a response signature.")
  public Response publish(
      final Event event
  ) {
    for (Subscription subscription : subscriptionRepository.getSubscriptionsForEventType(event.eventType)) {
      final String webhook = subscription.getWebHook();

      // TODO: better eventbus system
      // TODO: compute signature rather than sending the secret
      try {
        ClientBuilder.newClient().target(subscription.getWebHook()).request()
            .header(SECRET_HEADER, subscription.getSecret())
            .async().post(Entity.json(event), new InvocationCallback<Object>() {
              @Override
              public void completed(Object o) {<commit_msg>Fix webhook requests failures in EventBusEndpoint.<commit_after>package oasis.web.eventbus;

import java.net.URI;

import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.InvocationCallback;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Strings;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

import oasis.model.InvalidVersionException;
import oasis.model.applications.ApplicationRepository;
import oasis.model.applications.Subscription;
import oasis.model.applications.SubscriptionRepository;
import oasis.services.etag.EtagService;
import oasis.web.authn.Authenticated;
import oasis.web.authn.Client;
import oasis.web.providers.JacksonJsonProvider;
import oasis.web.utils.ResponseFactory;

@Authenticated
@Client
@Path("/e")
@Api(value = "/e", description = "EventBus API")
public class EventBusEndpoint {
  private static final Logger logger = LoggerFactory.getLogger(EventBusEndpoint.class);
  private static final String SECRET_HEADER = "X-Webhook-Secret";

  @Inject SubscriptionRepository subscriptionRepository;
  @Inject ApplicationRepository applicationRepository;
  @Inject EtagService etagService;

  @POST
  @Path("/publish")
  @Consumes(MediaType.APPLICATION_JSON)
  @ApiOperation(value = "Publish a typed event into the event bus.",
      notes = "The header '" + SECRET_HEADER + "' contains the secret defined by the application for this subscription." +
          "This will be replaced by a response signature.")
  public Response publish(
      final Event event
  ) {
    for (Subscription subscription : subscriptionRepository.getSubscriptionsForEventType(event.eventType)) {
      final String webhook = subscription.getWebHook();

      // TODO: better eventbus system
      // TODO: compute signature rather than sending the secret
      try {
        ClientBuilder.newClient()
            .register(JacksonJsonProvider.class)
            .target(subscription.getWebHook()).request()
            .header(SECRET_HEADER, subscription.getSecret())
            .async().post(Entity.json(event), new InvocationCallback<Object>() {
              @Override
              public void completed(Object o) {
{'fraction_non_alphanumeric': 0.10317002881844381, 'fraction_numerical': 0.0007684918347742555, 'mean_word_length': 4.491561181434599, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1890686', 'n_tokens_mistral': 1485, 'n_tokens_neox': 1490, 'n_words': 341}