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.transport.xmpp;
8   
9   import org.mule.api.endpoint.MalformedEndpointException;
10  import org.mule.endpoint.UserInfoEndpointURIBuilder;
11  import org.mule.transport.xmpp.i18n.XmppMessages;
12  
13  import java.net.URI;
14  import java.util.Properties;
15  
16  /**
17   * This endpoint builder ensures that a path is set on the URI as the path is the message type.
18   * 
19   */
20  public class XmppEndpointURIBuilder extends UserInfoEndpointURIBuilder
21  {
22      @Override
23      protected void setEndpoint(URI uri, Properties props) throws MalformedEndpointException
24      {
25          checkXmppMessageType(uri);
26          checkRecipient(uri);
27          super.setEndpoint(uri, props);
28      }
29  
30      private void checkXmppMessageType(URI uri) throws MalformedEndpointException
31      {
32          // the XMPP message type is stored in the host of the URL
33          String host = uri.getHost();
34          
35          if (host.length() == 0)
36          {
37              throw new MalformedEndpointException(XmppMessages.noMessageTypeInUri(), uri.toString());
38          }
39          
40          try
41          {
42              XmppMessageType.valueOf(host);
43          }
44          catch (IllegalArgumentException e)
45          {
46              throw new MalformedEndpointException(XmppMessages.invalidMessageTypeInUri(), 
47                  uri.toString());
48          }
49      }
50  
51      private void checkRecipient(URI uri) throws MalformedEndpointException
52      {
53          // the recipient is stored in the path of the URL
54          if (uri.getPath().length() == 0)
55          {
56              throw new MalformedEndpointException(XmppMessages.noRecipientInUri(), uri.toString());
57          }        
58      }
59  }