1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package complex.dispatches;
24 
25 // __________ Imports __________
26 
27 // structs, const, ...
28 import com.sun.star.beans.PropertyValue;
29 
30 // exceptions
31 import com.sun.star.frame.DispatchDescriptor;
32 import com.sun.star.frame.XDispatch;
33 import com.sun.star.frame.XDispatchProvider;
34 import com.sun.star.frame.XDispatchProviderInterceptor;
35 import com.sun.star.frame.XInterceptorInfo;
36 import com.sun.star.frame.XStatusListener;
37 
38 // interfaces
39 
40 
41 // helper
42 import com.sun.star.util.URL;
43 
44 // others
45 //import java.lang.*;
46 
47 // __________ Implementation __________
48 
49 /**
50  * implements a configurable interceptor for dispatch events.
51  */
52 public class Interceptor implements XDispatchProvider,
53                                     XDispatch,
54                                     XDispatchProviderInterceptor,
55                                     XInterceptorInfo
56 {
57     // ____________________
58 
59     /** contains the list of interception URL schema's (wildcards are allowed there!)
60         supported by this interceptor. It can be set from outside.
61         If no external URLs are set, the default "*" is used instead.
62         That would have the same effect as if this implementation would not support the
63         interface XInterceptorInfo !
64      */
65     private String[] m_lURLs4InterceptionInfo = null;
66 
67     // ____________________
68 
69     /** These URL's will be blocked by this interceptor.
70         Can be set from outside. Every queryDispatch() for these
71         set of URL's will be answered with an empty dispatch object!
72         If no external URLs are set the default "*" is used instead.
73         So every incoming URL will be blocked .-)
74      */
75     private String[] m_lURLs4Blocking = null;
76 
77     // ____________________
78 
79     /** Every dispatch interceptor knows it's master and slave interceptor
80         of the dispatch chain. These values must be stupid handled .-)
81         They have to be set and reset in case the right interface methods are called.
82         Nothing more. It's not allowed to dispose() it.
83         The slave can be used inside queryDispatch() to forward requests,
84         which are not handled by this interceptor instance.
85      */
86     private XDispatchProvider m_xSlave  = null;
87     private XDispatchProvider m_xMaster = null;
88 
89     // ____________________
90 
91     /** counts calls of setSlave...().
92         So the outside API test can use this value to know if this interceptor
93         was realy added to the interceptor chain of OOo.
94      */
95     private int m_nRegistrationCount = 0;
96 
97     // ____________________
98 
99     /** indicates if this interceptor object is currently part of the interceptor
100         chain of OOo. Only true if a valid slave or master dispatch is set on this
101         instance.
102      */
103     private boolean m_bIsRegistered = false;
104 
105 
106     // ____________________
107 
108     /** ctor
109      *  It's initialize an object of this class with default values.
110      */
Interceptor()111     public Interceptor()
112     {
113     }
114 
115     // ____________________
116 
117     /** XInterceptorInfo */
getInterceptedURLs()118     public synchronized String[] getInterceptedURLs()
119     {
120         return impl_getURLs4InterceptionInfo();
121     }
122 
123     // ____________________
124 
125     /** XDispatchProviderInterceptor */
getSlaveDispatchProvider()126     public synchronized XDispatchProvider getSlaveDispatchProvider()
127     {
128         System.out.println("Interceptor.getSlaveDispatchProvider() called");
129         return m_xSlave;
130     }
131 
132     // ____________________
133 
134     /** XDispatchProviderInterceptor */
getMasterDispatchProvider()135     public synchronized XDispatchProvider getMasterDispatchProvider()
136     {
137         System.out.println("Interceptor.getMasterDispatchProvider() called");
138         return m_xMaster;
139     }
140 
141     // ____________________
142 
143     /** XDispatchProviderInterceptor */
setSlaveDispatchProvider(XDispatchProvider xSlave)144     public synchronized void setSlaveDispatchProvider(XDispatchProvider xSlave)
145     {
146         System.out.println("Interceptor.setSlaveDispatchProvider("+xSlave+") called");
147 
148         if (xSlave != null)
149         {
150             ++m_nRegistrationCount;
151             m_bIsRegistered = true;
152         }
153         else
154         {
155             m_bIsRegistered = false;
156         }
157 
158         m_xSlave = xSlave;
159     }
160 
161     // ____________________
162 
163     /** XDispatchProviderInterceptor */
setMasterDispatchProvider(XDispatchProvider xMaster)164     public synchronized void setMasterDispatchProvider(XDispatchProvider xMaster)
165     {
166         System.out.println("Interceptor.setMasterDispatchProvider("+xMaster+") called");
167         m_xMaster = xMaster;
168     }
169 
170     // ____________________
171 
172     /** XDispatchProvider
173      */
queryDispatch(URL aURL , String sTargetFrameName, int nSearchFlags )174     public synchronized XDispatch queryDispatch(URL aURL            ,
175                                                                    String                sTargetFrameName,
176                                                                    int                   nSearchFlags    )
177     {
178         System.out.println("Interceptor.queryDispatch('"+aURL.Complete+"', '"+sTargetFrameName+"', "+nSearchFlags+") called");
179 
180         if (impl_isBlockedURL(aURL.Complete))
181         {
182             System.out.println("Interceptor.queryDispatch(): URL blocked => returns NULL");
183             return null;
184         }
185 
186         if (m_xSlave != null)
187         {
188             System.out.println("Interceptor.queryDispatch(): ask slave ...");
189             return m_xSlave.queryDispatch(aURL, sTargetFrameName, nSearchFlags);
190         }
191 
192         System.out.println("Interceptor.queryDispatch(): no idea => returns this");
193         return this;
194     }
195 
196     // ____________________
197 
198     /** XDispatchProvider
199      */
queryDispatches(DispatchDescriptor[] lRequests)200     public XDispatch[] queryDispatches(DispatchDescriptor[] lRequests)
201     {
202         int i = 0;
203         int c = lRequests.length;
204 
205         XDispatch[] lResults = new XDispatch[c];
206         for (i=0; i<c; ++i)
207         {
208             lResults[i] = queryDispatch(lRequests[i].FeatureURL ,
209                                         lRequests[i].FrameName  ,
210                                         lRequests[i].SearchFlags);
211         }
212 
213         return lResults;
214     }
215 
216     // ____________________
217 
218     /** XDispatch
219      */
dispatch(URL aURL , PropertyValue[] lArguments)220     public synchronized void dispatch(URL              aURL      ,
221                                       PropertyValue[] lArguments)
222     {
223         System.out.println("Interceptor.dispatch('"+aURL.Complete+"') called");
224     }
225 
226     // ____________________
227 
228     /** XDispatch
229      */
addStatusListener(XStatusListener xListener, com.sun.star.util.URL aURL )230     public synchronized void addStatusListener(XStatusListener xListener,
231                                                com.sun.star.util.URL              aURL     )
232     {
233         System.out.println("Interceptor.addStatusListener(..., '"+aURL.Complete+"') called");
234     }
235 
236     // ____________________
237 
238     /** XDispatch
239      */
removeStatusListener(XStatusListener xListener, com.sun.star.util.URL aURL )240     public synchronized void removeStatusListener(XStatusListener xListener,
241                                                   com.sun.star.util.URL              aURL     )
242     {
243         System.out.println("Interceptor.removeStatusListener(..., '"+aURL.Complete+"') called");
244     }
245 
246     // ____________________
247 
getRegistrationCount()248     public synchronized int getRegistrationCount()
249     {
250         return m_nRegistrationCount;
251     }
252 
253     // ____________________
254 
isRegistered()255     public synchronized boolean isRegistered()
256     {
257         return m_bIsRegistered;
258     }
259 
260     // ____________________
261 
262     /** set a new list of URL's, which should be used on registration time
263         (that's why it's neccessary to call this impl-method before the interceptor
264         is used at the OOo API!) to optimize the interception chain.
265      */
setURLs4InterceptionInfo(String[] lURLs)266     public synchronized void setURLs4InterceptionInfo(String[] lURLs)
267     {
268         m_lURLs4InterceptionInfo = lURLs;
269     }
270 
271     // ____________________
272 
273     /** set a new list of URL's, which should be blocked by this interceptor.
274         (that's why it's neccessary to call this impl-method before the interceptor
275         is used at the OOo API!)
276      */
setURLs4URLs4Blocking(String[] lURLs)277     public synchronized void setURLs4URLs4Blocking(String[] lURLs)
278     {
279         m_lURLs4Blocking = lURLs;
280     }
281 
282     // ____________________
283 
284     /** must be used internal to access the member m_lURLs4InterceptionInfo
285         - threadsafe
286         - and to make sure it's initialized on demand
287      */
impl_getURLs4InterceptionInfo()288     private synchronized String[] impl_getURLs4InterceptionInfo()
289     {
290         if (m_lURLs4InterceptionInfo == null)
291         {
292             m_lURLs4InterceptionInfo    = new String[1];
293             m_lURLs4InterceptionInfo[0] = "*";
294         }
295 
296         return m_lURLs4InterceptionInfo;
297     }
298 
299     // ____________________
300 
301     /** must be used internal to access the member m_lURLs4Blocking
302         - threadsafe
303         - and to make sure it's initialized on demand
304      */
impl_getURLs4Blocking()305     private synchronized String[] impl_getURLs4Blocking()
306     {
307         if (m_lURLs4Blocking == null)
308         {
309             m_lURLs4Blocking    = new String[1];
310             m_lURLs4Blocking[0] = "*";
311         }
312 
313         return m_lURLs4Blocking;
314     }
315 
316     // ____________________
impl_isBlockedURL(String sURL)317     private boolean impl_isBlockedURL(String sURL)
318     {
319         String[] lBlockedURLs = impl_getURLs4Blocking();
320         int      i            = 0;
321         int      c            = lBlockedURLs.length;
322 
323         for (i=0; i<c; ++i)
324         {
325             if (impl_match(sURL, lBlockedURLs[i]))
326             {
327                 return true;
328             }
329         }
330 
331         return false;
332     }
333 
334     // ____________________
335 
impl_match(String sVal1, String sVal2)336     private boolean impl_match(String sVal1, String sVal2)
337     {
338         // TODO implement wildcard match
339         return (sVal1.equals(sVal2));
340     }
341 }
342