Ejemplo n.º 1
0
  @SuppressWarnings("unchecked")
  protected void registerReceiverWithMuleService(MessageReceiver receiver, EndpointURI ep)
      throws MuleException {
    CxfMessageReceiver cxfReceiver = (CxfMessageReceiver) receiver;
    Server server = cxfReceiver.getServer();

    uriToServer.put(server.getEndpoint().getEndpointInfo().getAddress(), server);

    // TODO MULE-2228 Simplify this API
    SedaService c = new SedaService();

    String uniqueServiceName = createServiceName(server.getEndpoint());
    c.setName(uniqueServiceName);

    c.setModel(muleContext.getRegistry().lookupSystemModel());

    CxfServiceComponent svcComponent = new CxfServiceComponent(this, (CxfMessageReceiver) receiver);
    svcComponent.setBus(bus);

    c.setComponent(new DefaultJavaComponent(new SingletonObjectFactory(svcComponent)));

    // No determine if the endpointUri requires a new connector to be
    // registed in the case of http we only need to register the new
    // endpointUri if the port is different
    String endpoint = receiver.getEndpointURI().getAddress();
    String scheme = ep.getScheme().toLowerCase();

    InboundEndpoint originalEndpoint = receiver.getEndpoint();
    boolean sync = originalEndpoint.isSynchronous();

    // If we are using sockets then we need to set the endpoint name appropiately
    // and if using http/https
    // we need to default to POST and set the Content-Type
    if (scheme.equals("http")
        || scheme.equals("https")
        || scheme.equals("ssl")
        || scheme.equals("tcp")
        || scheme.equals("servlet")) {
      originalEndpoint.getProperties().put(HttpConnector.HTTP_METHOD_PROPERTY, "POST");
      originalEndpoint.getProperties().put(HttpConstants.HEADER_CONTENT_TYPE, "text/xml");
    }

    QName serviceName = server.getEndpoint().getEndpointInfo().getName();

    EndpointBuilder protocolEndpointBuilder = new EndpointURIEndpointBuilder(endpoint, muleContext);
    protocolEndpointBuilder.setSynchronous(sync);
    protocolEndpointBuilder.setName(ep.getScheme() + ":" + serviceName.getLocalPart());
    protocolEndpointBuilder.setTransactionConfig(originalEndpoint.getTransactionConfig());

    EndpointBuilder receiverEndpointBuilder =
        new EndpointURIEndpointBuilder(originalEndpoint, muleContext);

    // Apply the transformers to the correct endpoint
    EndpointBuilder transformerEndpoint;
    if (cxfReceiver.isApplyTransformersToProtocol()) {
      transformerEndpoint = protocolEndpointBuilder;
      receiverEndpointBuilder.setTransformers(Collections.emptyList());
      receiverEndpointBuilder.setResponseTransformers(Collections.emptyList());
    } else {
      transformerEndpoint = receiverEndpointBuilder;
    }

    // Ensure that the transformers aren't empty before setting them. Otherwise Mule will get
    // confused
    // and won't add the default transformers.
    if (originalEndpoint.getTransformers() != null
        && !originalEndpoint.getTransformers().isEmpty()) {
      transformerEndpoint.setTransformers(originalEndpoint.getTransformers());
    }

    if (originalEndpoint.getResponseTransformers() != null
        && !originalEndpoint.getResponseTransformers().isEmpty()) {
      transformerEndpoint.setResponseTransformers(originalEndpoint.getResponseTransformers());
    }

    // apply the filters to the correct endpoint
    EndpointBuilder filterEndpoint;
    if (cxfReceiver.isApplyFiltersToProtocol()) {
      filterEndpoint = protocolEndpointBuilder;
      receiverEndpointBuilder.setFilter(null);
    } else {
      filterEndpoint = receiverEndpointBuilder;
    }
    filterEndpoint.setFilter(originalEndpoint.getFilter());

    // apply the security filter to the correct endpoint
    EndpointBuilder secFilterEndpoint;
    if (cxfReceiver.isApplySecurityToProtocol()) {
      secFilterEndpoint = protocolEndpointBuilder;
      receiverEndpointBuilder.setSecurityFilter(null);
    } else {
      secFilterEndpoint = receiverEndpointBuilder;
    }
    secFilterEndpoint.setSecurityFilter(originalEndpoint.getSecurityFilter());

    String connectorName = (String) originalEndpoint.getProperty(CxfConstants.PROTOCOL_CONNECTOR);
    if (connectorName != null) {
      protocolEndpointBuilder.setConnector(
          muleContext.getRegistry().lookupConnector(connectorName));
    }

    InboundEndpoint protocolEndpoint =
        muleContext
            .getRegistry()
            .lookupEndpointFactory()
            .getInboundEndpoint(protocolEndpointBuilder);

    InboundEndpoint receiverEndpoint =
        muleContext
            .getRegistry()
            .lookupEndpointFactory()
            .getInboundEndpoint(receiverEndpointBuilder);

    receiver.setEndpoint(receiverEndpoint);

    c.setInboundRouter(new DefaultInboundRouterCollection());
    c.getInboundRouter().addEndpoint(protocolEndpoint);
    services.add(c);
  }