summaryrefslogtreecommitdiff
path: root/qpid/java/client/src/main/java/org/apache/qpid/jms/DestinationStringParser.java
blob: 6a1637739d5e4088e1ed962b1640657822cbd72e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
package org.apache.qpid.jms;

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.qpid.framing.AMQShortString;
import org.apache.qpid.jms.QpidDestination.DestinationType;
import org.apache.qpid.messaging.Address;
import org.apache.qpid.messaging.address.AddressException;
import org.apache.qpid.messaging.address.Link;
import org.apache.qpid.messaging.address.Link.Reliability;
import org.apache.qpid.messaging.address.Node;
import org.apache.qpid.messaging.address.Node.AddressPolicy;
import org.apache.qpid.messaging.address.Node.NodeType;
import org.apache.qpid.messaging.util.AddressHelper;
import org.apache.qpid.url.AMQBindingURL;

public class DestinationStringParser
{
   public static Address parseAddressString(String str, DestinationType type) throws AddressException
   {
       Address addr = Address.parse(str);
       AddressHelper helper = new AddressHelper(addr);

       Node node = new Node();
       node.setName(addr.getName());
       node.setAssertPolicy(AddressPolicy.getAddressPolicy(helper.getAssert()));
       node.setCreatePolicy(AddressPolicy.getAddressPolicy(helper.getCreate()));
       node.setDeletePolicy(AddressPolicy.getAddressPolicy(helper.getDelete()));
       node.setDurable(helper.isNodeDurable());

       if (DestinationType.TOPIC == type)
       {
           if (helper.getNodeType() == NodeType.QUEUE)
           {
               throw new AddressException("Destination is marked as a Topic, but address is defined as a Queue");
           }
           node.setType(NodeType.TOPIC);
       }
       else
       {
           if (helper.getNodeType() == NodeType.TOPIC)
           {
               throw new AddressException("Destination is marked as a Queue, but address is defined as a Topic");
           }
           node.setType(NodeType.QUEUE);
       }

       node.setDeclareProps(helper.getNodeDeclareArgs());
       node.setBindingProps(helper.getNodeBindings());
       addr.setNode(node);

       Link link =  new Link();
       link.setName(helper.getLinkName());
       link.setDurable(helper.isLinkDurable());
       link.setReliability(Reliability.getReliability(helper.getLinkReliability()));
       link.setProducerCapacity(helper.getProducerCapacity());
       link.setConsumerCapacity(helper.getConsumeCapacity());
       link.setDeclareProps(helper.getLinkDeclareArgs());
       link.setBindingProps(helper.getLinkBindings());
       link.setSubscribeProps(helper.getLinkSubscribeArgs());
       addr.setLink(link);

       return addr;
   }

   public static Address parseBURLString(String str, DestinationType type) throws AddressException
   {
	   AMQBindingURL burl;
	   try
	   {
           burl = new AMQBindingURL(str);
	   }
	   catch(URISyntaxException e)
	   {
	       AddressException ex = new AddressException("Error parsing BURL : " + e.getMessage());
	       ex.initCause(e);
	       throw ex;
	   }

	   Address addr;
	   Node node = new Node();
	   Link link = new Link();

	   if (type == DestinationType.TOPIC)
	   {
	       addr = new Address(burl.getExchangeName().asString(),
                              burl.getRoutingKey().asString(),
                              Collections.emptyMap());

	      link.setName(burl.getQueueName().asString());
	      node.setBindingProps(Collections.emptyList());
	   }
	   else
	   {
		   addr = new Address(burl.getQueueName().asString(),
                              burl.getRoutingKey().asString(),
                              Collections.emptyMap());

		   List<Object> bindings = new ArrayList<Object>();
		   Map<String,Object> binding = new HashMap<String,Object>();
		   binding.put(AddressHelper.EXCHANGE, burl.getExchangeName().asString());
		   binding.put(AddressHelper.KEY, burl.getRoutingKey());
		   bindings.add(binding);
		   node.setBindingProps(bindings);
	   }

	   List<Object> bindings = node.getBindingProperties();
	   for (AMQShortString key: burl.getBindingKeys())
	   {
	       Map<String,Object> binding = new HashMap<String,Object>();
           binding.put(AddressHelper.EXCHANGE, burl.getExchangeName().asString());
           binding.put(AddressHelper.KEY, key.asString());
           bindings.add(binding);
	   }

	   node.setAssertPolicy(AddressPolicy.NEVER);
       node.setCreatePolicy(AddressPolicy.RECEIVER);
       node.setDeletePolicy(AddressPolicy.NEVER);

	   return addr;
   }
}