View Javadoc

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