Ejemplo n.º 1
0
  private <T> void setDispatchProperties(Dispatch<T> dispatch, String binding) {
    if (this.mcf.getAsSecurityType() == WSManagedConnectionFactory.SecurityType.HTTPBasic) {

      String userName = this.mcf.getAuthUserName();
      String password = this.mcf.getAuthPassword();

      // if security-domain is specified and caller identity is used; then use
      // credentials from subject
      Subject subject = ConnectionContext.getSubject();
      if (subject != null) {
        userName = ConnectionContext.getUserName(subject, this.mcf, userName);
        password = ConnectionContext.getPassword(subject, this.mcf, userName, password);
      }

      dispatch.getRequestContext().put(Dispatch.USERNAME_PROPERTY, userName);
      dispatch.getRequestContext().put(Dispatch.PASSWORD_PROPERTY, password);
    } else if (this.mcf.getAsSecurityType() == WSManagedConnectionFactory.SecurityType.Kerberos) {
      boolean credentialFound = false;
      Subject subject = ConnectionContext.getSubject();
      if (subject != null) {
        GSSCredential credential =
            ConnectionContext.getSecurityCredential(subject, GSSCredential.class);
        if (credential != null) {
          dispatch.getRequestContext().put(GSSCredential.class.getName(), credential);
          credentialFound = true;
        }
      }
      if (!credentialFound) {
        throw new WebServiceException(
            WSManagedConnectionFactory.UTIL.getString("no_gss_credential")); // $NON-NLS-1$
      }
    } else if (this.mcf.getAsSecurityType() == WSManagedConnectionFactory.SecurityType.OAuth) {
      boolean credentialFound = false;
      Subject subject = ConnectionContext.getSubject();
      if (subject != null) {
        OAuthCredential credential =
            ConnectionContext.getSecurityCredential(subject, OAuthCredential.class);
        if (credential != null) {
          dispatch.getRequestContext().put(OAuthCredential.class.getName(), credential);
          credentialFound = true;
        }
      }
      if (!credentialFound) {
        throw new WebServiceException(
            WSManagedConnectionFactory.UTIL.getString("no_oauth_credential")); // $NON-NLS-1$
      }
    }

    if (this.mcf.getRequestTimeout() != null) {
      dispatch.getRequestContext().put(RECEIVE_TIMEOUT, this.mcf.getRequestTimeout());
    }
    if (this.mcf.getConnectTimeout() != null) {
      dispatch.getRequestContext().put(CONNECTION_TIMEOUT, this.mcf.getConnectTimeout());
    }

    if (HTTPBinding.HTTP_BINDING.equals(binding)) {
      Map<String, List<String>> httpHeaders =
          (Map<String, List<String>>)
              dispatch.getRequestContext().get(MessageContext.HTTP_REQUEST_HEADERS);
      if (httpHeaders == null) {
        httpHeaders = new HashMap<String, List<String>>();
      }
      httpHeaders.put(
          "Content-Type",
          Collections.singletonList("text/xml; charset=utf-8")); // $NON-NLS-1$ //$NON-NLS-2$
      httpHeaders.put(
          "User-Agent", Collections.singletonList("Teiid Server")); // $NON-NLS-1$ //$NON-NLS-2$
      dispatch.getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS, httpHeaders);
    }
  }
Ejemplo n.º 2
0
  public <T> Dispatch<T> createDispatch(String binding, String endpoint, Class<T> type, Mode mode) {
    ArgCheck.isNotNull(binding);
    if (endpoint != null) {
      try {
        new URL(endpoint);
        // valid url, just use the endpoint
      } catch (MalformedURLException e) {
        // otherwise it should be a relative value
        // but we should still preserve the base path and query string
        String defaultEndpoint = this.mcf.getEndPoint();
        String defaultQueryString = null;
        String defaultFragment = null;
        if (defaultEndpoint == null) {
          throw new WebServiceException(
              WSManagedConnectionFactory.UTIL.getString("null_default_endpoint")); // $NON-NLS-1$
        }
        String[] parts = defaultEndpoint.split("\\?", 2); // $NON-NLS-1$
        defaultEndpoint = parts[0];
        if (parts.length > 1) {
          defaultQueryString = parts[1];
          parts = defaultQueryString.split("#"); // $NON-NLS-1$
          defaultQueryString = parts[0];
          if (parts.length > 1) {
            defaultFragment = parts[1];
          }
        }
        if (endpoint.startsWith("?") || endpoint.startsWith("/")) { // $NON-NLS-1$ //$NON-NLS-2$
          endpoint = defaultEndpoint + endpoint;
        } else {
          endpoint = defaultEndpoint + "/" + endpoint; // $NON-NLS-1$
        }
        if ((defaultQueryString != null) && (defaultQueryString.trim().length() > 0)) {
          endpoint = WSConnection.Util.appendQueryString(endpoint, defaultQueryString);
        }
        if ((defaultFragment != null) && (endpoint.indexOf('#') < 0)) {
          endpoint = endpoint + '#' + defaultFragment;
        }
      }
    } else {
      endpoint = this.mcf.getEndPoint();
      if (endpoint == null) {
        throw new WebServiceException(
            WSManagedConnectionFactory.UTIL.getString("null_endpoint")); // $NON-NLS-1$
      }
    }
    Dispatch<T> dispatch = null;
    if (HTTPBinding.HTTP_BINDING.equals(binding) && (type == DataSource.class)) {
      Bus bus = BusFactory.getThreadDefaultBus();
      BusFactory.setThreadDefaultBus(this.mcf.getBus());
      try {
        dispatch =
            (Dispatch<T>)
                new HttpDispatch(endpoint, this.mcf.getConfigFile(), this.mcf.getConfigName());
      } finally {
        BusFactory.setThreadDefaultBus(bus);
      }
    } else {
      // TODO: cache service/port/dispatch instances?
      Bus bus = BusFactory.getThreadDefaultBus();
      BusFactory.setThreadDefaultBus(this.mcf.getBus());
      Service svc;
      try {
        svc = Service.create(this.mcf.getServiceQName());
      } finally {
        BusFactory.setThreadDefaultBus(bus);
      }
      if (LogManager.isMessageToBeRecorded(LogConstants.CTX_WS, MessageLevel.DETAIL)) {
        LogManager.logDetail(
            LogConstants.CTX_WS, "Creating a dispatch with endpoint", endpoint); // $NON-NLS-1$
      }
      svc.addPort(this.mcf.getPortQName(), binding, endpoint);

      dispatch = svc.createDispatch(this.mcf.getPortQName(), type, mode);
      configureWSSecurity(dispatch);
    }
    setDispatchProperties(dispatch, binding);
    return dispatch;
  }