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