View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.endpoint;
8   
9   import org.mule.api.endpoint.MalformedEndpointException;
10  
11  import java.net.URI;
12  import java.util.Properties;
13  
14  /**
15   * <code>UserInfoEndpointBuilder</code> builds an endpoint with the userinfo and
16   * host details. This endpoint builder is used where endpoints as of the form :
17   * xxx://ross:secret@host:1000
18   */
19  public class UserInfoEndpointURIBuilder extends AbstractEndpointURIBuilder
20  {
21      //TODO THis endpoint builder is redundant I think. We should be able to use the URL endpoint builder.
22      //It depends on where deriving classes can work with the URL endpoint builder, but there are a lot of similarities
23      protected void setEndpoint(URI uri, Properties props) throws MalformedEndpointException
24      {
25          // Added by Lajos 2006-12-14 per Ross
26          if (uri.getHost() == null)
27          {
28              if (props.getProperty("address") == null)
29              {
30                  throw new MalformedEndpointException(uri.toString());
31              }
32              else
33              {
34                  return;
35              }
36          }
37  
38          // Check and handle '@' symbols in the user info
39          address = uri.getHost();
40          int a = address.indexOf(".");
41          int b = (a == -1 ? -1 : address.indexOf(".", a + 1));
42          if (b > -1)
43          {
44              address = address.substring(a + 1);
45          }
46  
47          if (uri.getPort() != -1)
48          {
49              // set the endpointUri to be a proper url if host and port are set
50              this.address += ":" + uri.getPort();
51          }
52  
53          if (userInfo != null)
54          {
55              int x = userInfo.indexOf(":");
56              if (x > -1)
57              {
58                  String user = userInfo.substring(0, x);
59                  if (user.indexOf("@") > -1)
60                  {
61                      address = user;
62                  }
63                  else
64                  {
65                      address = user + "@" + address;
66                  }
67              }
68              else
69              {
70                  if (userInfo.indexOf("@") > -1)
71                  {
72                      address = userInfo;
73                  }
74                  else
75                  {
76                      address = userInfo + "@" + address;
77                  }
78              }
79          }
80      }
81  }