View Javadoc

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