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  import org.mule.util.StringUtils;
11  
12  import java.net.URI;
13  import java.util.Properties;
14  
15  import org.apache.commons.logging.Log;
16  import org.apache.commons.logging.LogFactory;
17  
18  /**
19   * <code>ResourceNameEndpointBuilder</code> extracts a resource name from a uri
20   * endpointUri
21   * 
22   */
23  public class ResourceNameEndpointURIBuilder extends AbstractEndpointURIBuilder
24  {
25  
26      protected static final Log logger = LogFactory.getLog(ResourceNameEndpointURIBuilder.class);
27      
28      public static final String RESOURCE_INFO_PROPERTY = "resourceInfo";
29  
30      protected void setEndpoint(URI uri, Properties props) throws MalformedEndpointException
31      {
32          address = StringUtils.EMPTY;
33          String host = uri.getHost();
34          if (host != null && !"localhost".equals(host))
35          {
36              address = host;
37          }
38  
39          String path = uri.getPath();
40          String authority = uri.getAuthority();
41          
42          if (path != null && path.length() != 0)
43          {
44              if (address.length() > 0)
45              {
46                  address += "/";
47              }
48              address += path.substring(1);
49          }
50          else if (authority != null && !authority.equals(address))
51          {
52              address += authority;
53              
54              int atCharIndex = -1;
55              if (address != null && address.length() != 0 && ((atCharIndex = address.indexOf("@")) > -1))
56              {
57                  userInfo = address.substring(0, atCharIndex);
58                  address = address.substring(atCharIndex + 1);
59              }
60  
61          }
62          
63          // is user info specified?
64          int y = address.indexOf("@");
65          if (y > -1)
66          {
67              userInfo = address.substring(0, y);
68          }
69          // increment to 0 or one char past the @
70          y++;
71  
72          String credentials = uri.getUserInfo();
73          if (credentials != null && credentials.length() != 0)
74          {
75              userInfo = credentials;
76          }
77          
78          int x = address.indexOf(":", y);
79          if (x > -1)
80          {
81              String resourceInfo = address.substring(y, x);
82              props.setProperty(RESOURCE_INFO_PROPERTY, resourceInfo);
83              address = address.substring(x + 1);
84          }
85      }
86  }