Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8b69647a9734fb1b33120094e49ffd0127b9e9aa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * Copyright (c) 2008-2012, 2015, 2016 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.net4j.connector;

import org.eclipse.net4j.util.security.INegotiatorAware;
import org.eclipse.net4j.util.security.IPasswordCredentialsProvider;
import org.eclipse.net4j.util.security.ResponseNegotiator;
import org.eclipse.net4j.util.security.ResponseNegotiatorInjector;
import org.eclipse.net4j.util.security.SecurityUtil;

/**
 * Injects a configurable response negotiator into selected client connectors.
 * <p>
 * An example:
 *
 * <pre>
 * IManagedContainer container = IPluginContainer.INSTANCE;
 *
 * String connectorDescription = &quot;localhost:2036&quot;;
 * String userID = &quot;name&quot;;
 * String password = &quot;secret&quot;;
 *
 * IPasswordCredentialsProvider credentialsProvider = new PasswordCredentialsProvider(userID, password);
 *
 * container.addPostProcessor(new ConnectorCredentialsInjector(connectorDescription, credentialsProvider));
 * IConnector connector = (IConnector)container.getElement(&quot;org.eclipse.net4j.connectors&quot;, &quot;tcp&quot;, connectorDescription);
 *
 * IChannel channel = connector.openChannel();
 * // ...
 * </pre>
 *
 * @author Eike Stepper
 * @since 2.0
 */
public class ConnectorCredentialsInjector extends ResponseNegotiatorInjector
{
  private String connectorDescription;

  /**
   * @param connectorDescription
   *          The description of the IConnector that the negotiator shall be injected into, or <code>null</code> to
   *          bypass the description check.
   */
  public ConnectorCredentialsInjector(String connectorDescription, IPasswordCredentialsProvider credentialsProvider, String algorithmName)
  {
    super(createNegotiator(credentialsProvider, algorithmName));
    this.connectorDescription = connectorDescription;
  }

  /**
   * @param connectorDescription
   *          The description of the IConnector that the negotiator shall be injected into, or <code>null</code> to
   *          bypass the description check.
   */
  public ConnectorCredentialsInjector(String connectorDescription, IPasswordCredentialsProvider credentialsProvider)
  {
    this(connectorDescription, credentialsProvider, SecurityUtil.PBE_WITH_MD5_AND_DES);
  }

  @Override
  protected boolean filterElement(String productGroup, String factoryType, String description, INegotiatorAware negotiatorAware)
  {
    if (negotiatorAware instanceof IConnector)
    {
      IConnector connector = (IConnector)negotiatorAware;
      if (connector.isClient())
      {
        return filterConnectorDescription(description);
      }
    }

    return false;
  }

  protected boolean filterConnectorDescription(String description)
  {
    if (connectorDescription == null)
    {
      return true;
    }

    return connectorDescription.equals(description);
  }

  private static ResponseNegotiator createNegotiator(IPasswordCredentialsProvider credentialsProvider, String algorithmName)
  {
    ResponseNegotiator negotiator = new ResponseNegotiator();
    negotiator.setCredentialsProvider(credentialsProvider);
    negotiator.setEncryptionAlgorithmName(algorithmName);
    return negotiator;
  }
}

Back to the top