1 /*******************************************************************************
\r
2 * ============LICENSE_START==================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
25 package com.att.research.datarouter.node;
\r
31 * Track redirections of subscriptions
\r
33 public class RedirManager {
\r
34 private Hashtable<String, String> sid2primary = new Hashtable<String, String>();
\r
35 private Hashtable<String, String> sid2secondary = new Hashtable<String, String>();
\r
36 private String redirfile;
\r
37 RateLimitedOperation op;
\r
39 * Create a mechanism for maintaining subscription redirections.
\r
40 * @param redirfile The file to store the redirection information.
\r
41 * @param mininterval The minimum number of milliseconds between writes to the redirection information file.
\r
42 * @param timer The timer thread used to run delayed file writes.
\r
44 public RedirManager(String redirfile, long mininterval, Timer timer) {
\r
45 this.redirfile = redirfile;
\r
46 op = new RateLimitedOperation(mininterval, timer) {
\r
49 StringBuffer sb = new StringBuffer();
\r
50 for (String s: sid2primary.keySet()) {
\r
51 sb.append(s).append(' ').append(sid2primary.get(s)).append(' ').append(sid2secondary.get(s)).append('\n');
\r
53 OutputStream os = new FileOutputStream(RedirManager.this.redirfile);
\r
54 os.write(sb.toString().getBytes());
\r
56 } catch (Exception e) {
\r
62 BufferedReader br = new BufferedReader(new FileReader(redirfile));
\r
63 while ((s = br.readLine()) != null) {
\r
65 String[] sx = s.split(" ");
\r
66 if (s.startsWith("#") || sx.length != 3) {
\r
69 sid2primary.put(sx[0], sx[1]);
\r
70 sid2secondary.put(sx[0], sx[2]);
\r
73 } catch (Exception e) {
\r
74 // missing file is normal
\r
78 * Set up redirection. If a request is to be sent to subscription ID sid, and that is configured to go to URL primary, instead, go to secondary.
\r
79 * @param sid The subscription ID to be redirected
\r
80 * @param primary The URL associated with that subscription ID
\r
81 * @param secondary The replacement URL to use instead
\r
83 public synchronized void redirect(String sid, String primary, String secondary) {
\r
84 sid2primary.put(sid, primary);
\r
85 sid2secondary.put(sid, secondary);
\r
89 * Cancel redirection. If a request is to be sent to subscription ID sid, send it to its primary URL.
\r
90 * @param sid The subscription ID to remove from the table.
\r
92 public synchronized void forget(String sid) {
\r
93 sid2primary.remove(sid);
\r
94 sid2secondary.remove(sid);
\r
98 * Look up where to send a subscription. If the primary has changed or there is no redirection, use the primary. Otherwise, redirect to the secondary URL.
\r
99 * @param sid The subscription ID to look up.
\r
100 * @param primary The configured primary URL.
\r
101 * @return The destination URL to really use.
\r
103 public synchronized String lookup(String sid, String primary) {
\r
104 String oprim = sid2primary.get(sid);
\r
105 if (primary.equals(oprim)) {
\r
106 return(sid2secondary.get(sid));
\r
107 } else if (oprim != null) {
\r
113 * Is a subscription redirected?
\r
115 public synchronized boolean isRedirected(String sid) {
\r
116 return(sid != null && sid2secondary.get(sid) != null);
\r