Source Code Cross Referenced for PolicyUtils.java in  » Web-Services-apache-cxf-2.0.1 » transports » org » apache » cxf » transport » http » policy » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Services apache cxf 2.0.1 » transports » org.apache.cxf.transport.http.policy 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Licensed to the Apache Software Foundation (ASF) under one
003:         * or more contributor license agreements. See the NOTICE file
004:         * distributed with this work for additional information
005:         * regarding copyright ownership. The ASF licenses this file
006:         * to you under the Apache License, Version 2.0 (the
007:         * "License"); you may not use this file except in compliance
008:         * with the License. You may obtain a copy of the License at
009:         *
010:         * http://www.apache.org/licenses/LICENSE-2.0
011:         *
012:         * Unless required by applicable law or agreed to in writing,
013:         * software distributed under the License is distributed on an
014:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015:         * KIND, either express or implied. See the License for the
016:         * specific language governing permissions and limitations
017:         * under the License.
018:         */package org.apache.cxf.transport.http.policy;
019:
020:        import java.util.ArrayList;
021:        import java.util.Collection;
022:        import java.util.logging.Level;
023:        import java.util.logging.Logger;
024:
025:        import javax.xml.namespace.QName;
026:
027:        import org.apache.cxf.common.logging.LogUtils;
028:        import org.apache.cxf.message.Message;
029:        import org.apache.cxf.message.MessageUtils;
030:        import org.apache.cxf.service.model.EndpointInfo;
031:        import org.apache.cxf.transport.Conduit;
032:        import org.apache.cxf.transport.Destination;
033:        import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
034:        import org.apache.cxf.transports.http.configuration.HTTPServerPolicy;
035:        import org.apache.cxf.ws.policy.AssertionInfo;
036:        import org.apache.cxf.ws.policy.AssertionInfoMap;
037:        import org.apache.cxf.ws.policy.PolicyEngine;
038:        import org.apache.cxf.ws.policy.PolicyException;
039:        import org.apache.cxf.ws.policy.builder.jaxb.JaxbAssertion;
040:        import org.apache.neethi.Assertion;
041:
042:        /**
043:         * 
044:         */
045:        public final class PolicyUtils {
046:
047:            public static final String HTTPCONF_NAMESPACE = "http://cxf.apache.org/transports/http/configuration";
048:            public static final QName HTTPCLIENTPOLICY_ASSERTION_QNAME = new QName(
049:                    HTTPCONF_NAMESPACE, "client");
050:            public static final QName HTTPSERVERPOLICY_ASSERTION_QNAME = new QName(
051:                    HTTPCONF_NAMESPACE, "server");
052:
053:            private static final Logger LOG = LogUtils
054:                    .getL7dLogger(PolicyUtils.class);
055:
056:            /**
057:             * Prevents instantiation.
058:             *
059:             */
060:            private PolicyUtils() {
061:            }
062:
063:            /**
064:             * Returns a HTTPClientPolicy that is compatible with the assertions included in the
065:             * service, endpoint, operation and message policy subjects AND the HTTPClientPolicy 
066:             * passed as a second argument.
067:             * @param message the message
068:             * @param confPolicy the additional policy to be compatible with
069:             * @return the HTTPClientPolicy for the message
070:             * @throws PolicyException if no compatible HTTPClientPolicy can be determined
071:             */
072:            public static HTTPClientPolicy getClient(Message message,
073:                    HTTPClientPolicy confPolicy) {
074:                AssertionInfoMap amap = message.get(AssertionInfoMap.class);
075:                if (null == amap) {
076:                    return confPolicy;
077:                }
078:                Collection<AssertionInfo> ais = amap
079:                        .get(HTTPCLIENTPOLICY_ASSERTION_QNAME);
080:                if (null == ais) {
081:                    return confPolicy;
082:                }
083:                Collection<Assertion> alternative = new ArrayList<Assertion>();
084:                for (AssertionInfo ai : ais) {
085:                    alternative.add(ai.getAssertion());
086:                }
087:                HTTPClientPolicy compatible = getClient(alternative);
088:                if (null != compatible && null != confPolicy) {
089:                    if (PolicyUtils.compatible(compatible, confPolicy)) {
090:                        compatible = intersect(compatible, confPolicy);
091:                    } else {
092:                        LogUtils.log(LOG, Level.SEVERE,
093:                                "INCOMPATIBLE_HTTPCLIENTPOLICY_ASSERTIONS");
094:                        throw new PolicyException(
095:                                new org.apache.cxf.common.i18n.Message(
096:                                        "INCOMPATIBLE_HTTPCLIENTPOLICY_ASSERTIONS",
097:                                        LOG));
098:                    }
099:                }
100:                return compatible;
101:            }
102:
103:            /**
104:             * Returns a HTTPServerPolicy that is compatible with the assertions included in the
105:             * service, endpoint, operation and message policy subjects AND the HTTPServerPolicy 
106:             * passed as a second argument.
107:             * @param message the message
108:             * @param confPolicy the additional policy to be compatible with
109:             * @return the HTTPServerPolicy for the message
110:             * @throws PolicyException if no compatible HTTPServerPolicy can be determined
111:             */
112:            public static HTTPServerPolicy getServer(Message message,
113:                    HTTPServerPolicy confPolicy) {
114:                AssertionInfoMap amap = message.get(AssertionInfoMap.class);
115:                if (null == amap) {
116:                    return confPolicy;
117:                }
118:                Collection<AssertionInfo> ais = amap
119:                        .get(HTTPSERVERPOLICY_ASSERTION_QNAME);
120:                if (null == ais) {
121:                    return confPolicy;
122:                }
123:                Collection<Assertion> alternative = new ArrayList<Assertion>();
124:                for (AssertionInfo ai : ais) {
125:                    alternative.add(ai.getAssertion());
126:                }
127:                HTTPServerPolicy compatible = getServer(alternative);
128:                if (null != compatible && null != confPolicy) {
129:                    if (PolicyUtils.compatible(compatible, confPolicy)) {
130:                        compatible = intersect(compatible, confPolicy);
131:                    } else {
132:                        LogUtils.log(LOG, Level.SEVERE,
133:                                "INCOMPATIBLE_HTTPSERVERPOLICY_ASSERTIONS");
134:                        throw new PolicyException(
135:                                new org.apache.cxf.common.i18n.Message(
136:                                        "INCOMPATIBLE_HTTPSERVERPOLICY_ASSERTIONS",
137:                                        LOG));
138:                    }
139:                }
140:                return compatible;
141:            }
142:
143:            /**
144:             * Returns a HTTPClientPolicy that is compatible with the assertions included in the
145:             * service and endpoint policy subjects, or null if there are no such assertions.
146:             * @param pe the policy engine
147:             * @param ei the endpoint info
148:             * @param c the conduit
149:             * @return the compatible policy
150:             * @throws PolicyException if no compatible HTTPClientPolicy can be determined
151:             */
152:            public static HTTPClientPolicy getClient(PolicyEngine pe,
153:                    EndpointInfo ei, Conduit c) {
154:                Collection<Assertion> alternative = pe.getClientEndpointPolicy(
155:                        ei, c).getChosenAlternative();
156:                HTTPClientPolicy compatible = null;
157:                for (Assertion a : alternative) {
158:                    if (HTTPCLIENTPOLICY_ASSERTION_QNAME.equals(a.getName())) {
159:                        HTTPClientPolicy p = JaxbAssertion.cast(a,
160:                                HTTPClientPolicy.class).getData();
161:                        if (null == compatible) {
162:                            compatible = p;
163:                        } else {
164:                            compatible = intersect(compatible, p);
165:                            if (null == compatible) {
166:                                LogUtils
167:                                        .log(LOG, Level.SEVERE,
168:                                                "INCOMPATIBLE_HTTPCLIENTPOLICY_ASSERTIONS");
169:                                throw new PolicyException(
170:                                        new org.apache.cxf.common.i18n.Message(
171:                                                "INCOMPATIBLE_HTTPCLIENTPOLICY_ASSERTIONS",
172:                                                LOG));
173:                            }
174:                        }
175:                    }
176:                }
177:                return compatible;
178:            }
179:
180:            /**
181:             * Returns a HTTPServerPolicy that is compatible with the assertions included in the
182:             * service and endpoint policy subjects, or null if there are no such assertions.
183:             * @param pe the policy engine
184:             * @param ei the endpoint info
185:             * @param d the destination
186:             * @return the compatible policy
187:             * @throws PolicyException if no compatible HTTPServerPolicy can be determined
188:             */
189:            public static HTTPServerPolicy getServer(PolicyEngine pe,
190:                    EndpointInfo ei, Destination d) {
191:                Collection<Assertion> alternative = pe.getServerEndpointPolicy(
192:                        ei, d).getChosenAlternative();
193:                HTTPServerPolicy compatible = null;
194:                for (Assertion a : alternative) {
195:                    if (HTTPSERVERPOLICY_ASSERTION_QNAME.equals(a.getName())) {
196:                        HTTPServerPolicy p = JaxbAssertion.cast(a,
197:                                HTTPServerPolicy.class).getData();
198:                        if (null == compatible) {
199:                            compatible = p;
200:                        } else {
201:                            compatible = intersect(compatible, p);
202:                            if (null == compatible) {
203:                                LogUtils
204:                                        .log(LOG, Level.SEVERE,
205:                                                "INCOMPATIBLE_HTTPSERVERPOLICY_ASSERTIONS");
206:                                throw new PolicyException(
207:                                        new org.apache.cxf.common.i18n.Message(
208:                                                "INCOMPATIBLE_HTTPSERVERPOLICY_ASSERTIONS",
209:                                                LOG));
210:                            }
211:                        }
212:                    }
213:                }
214:                return compatible;
215:            }
216:
217:            /**
218:             * Asserts all HTTPClientPolicy assertions that are compatible with the specified
219:             * client policy.
220:             * @param message the current message
221:             * @param client the client policy
222:             */
223:            public static void assertClientPolicy(Message message,
224:                    HTTPClientPolicy client) {
225:
226:                AssertionInfoMap aim = message.get(AssertionInfoMap.class);
227:                if (null == aim) {
228:                    return;
229:                }
230:                Collection<AssertionInfo> ais = aim
231:                        .get(HTTPCLIENTPOLICY_ASSERTION_QNAME);
232:                if (null == ais || ais.size() == 0) {
233:                    return;
234:                }
235:
236:                // assert all assertion(s) that are compatible with the value configured for the conduit
237:
238:                if (MessageUtils.isOutbound(message)) {
239:                    for (AssertionInfo ai : ais) {
240:                        HTTPClientPolicy p = (JaxbAssertion.cast(ai
241:                                .getAssertion(), HTTPClientPolicy.class))
242:                                .getData();
243:                        if (compatible(p, client)) {
244:                            ai.setAsserted(true);
245:                        }
246:                    }
247:                } else {
248:                    for (AssertionInfo ai : ais) {
249:                        ai.setAsserted(true);
250:                    }
251:                }
252:            }
253:
254:            /**
255:             * Asserts all HTTPServerPolicy assertions that are equal to the specified
256:             * server policy.
257:             * @param message the current message
258:             * @param server the server policy
259:             */
260:            public static void assertServerPolicy(Message message,
261:                    HTTPServerPolicy server) {
262:
263:                AssertionInfoMap aim = message.get(AssertionInfoMap.class);
264:                if (null == aim) {
265:                    return;
266:                }
267:                Collection<AssertionInfo> ais = aim
268:                        .get(HTTPSERVERPOLICY_ASSERTION_QNAME);
269:                if (null == ais || ais.size() == 0) {
270:                    return;
271:                }
272:
273:                // assert all assertion(s) that are equal to the value configured for the conduit
274:
275:                if (MessageUtils.isOutbound(message)) {
276:                    for (AssertionInfo ai : ais) {
277:                        ai.setAsserted(true);
278:                    }
279:                } else {
280:                    for (AssertionInfo ai : ais) {
281:                        HTTPServerPolicy p = (JaxbAssertion.cast(ai
282:                                .getAssertion(), HTTPServerPolicy.class))
283:                                .getData();
284:                        if (equals(p, server)) {
285:                            ai.setAsserted(true);
286:                        }
287:                    }
288:                }
289:            }
290:
291:            /**
292:             * Checks if two HTTPClientPolicy objects are compatible.
293:             * @param p1 one client policy
294:             * @param p2 another client policy
295:             * @return true iff policies are compatible
296:             */
297:            public static boolean compatible(HTTPClientPolicy p1,
298:                    HTTPClientPolicy p2) {
299:
300:                if (p1 == p2 || p1.equals(p2)) {
301:                    return true;
302:                }
303:
304:                boolean compatible = true;
305:
306:                if (compatible) {
307:                    compatible &= compatible(p1.getAccept(), p2.getAccept());
308:                }
309:
310:                if (compatible) {
311:                    compatible &= compatible(p1.getAcceptEncoding(), p2
312:                            .getAcceptEncoding());
313:                }
314:
315:                if (compatible) {
316:                    compatible &= compatible(p1.getAcceptLanguage(), p2
317:                            .getAcceptLanguage());
318:                }
319:
320:                if (compatible) {
321:                    compatible &= compatible(p1.getBrowserType(), p2
322:                            .getBrowserType());
323:                }
324:
325:                if (compatible) {
326:                    compatible &= !p1.isSetCacheControl()
327:                            || !p2.isSetCacheControl()
328:                            || p1.getCacheControl().value().equals(
329:                                    p2.getCacheControl().value());
330:                }
331:
332:                if (compatible) {
333:                    compatible = !p1.isSetConnection()
334:                            || !p2.isSetConnection()
335:                            || p1.getConnection().value().equals(
336:                                    p2.getConnection().value());
337:                }
338:
339:                if (compatible) {
340:                    compatible &= p1.getContentType().equals(
341:                            p2.getContentType());
342:                }
343:
344:                if (compatible) {
345:                    compatible &= compatible(p1.getCookie(), p2.getCookie());
346:                }
347:
348:                // REVISIT: Should compatibility require strict equality?
349:
350:                if (compatible) {
351:                    compatible &= compatible(p1.getDecoupledEndpoint(), p2
352:                            .getDecoupledEndpoint());
353:                }
354:
355:                if (compatible) {
356:                    compatible &= compatible(p1.getHost(), p2.getHost());
357:                }
358:
359:                if (compatible) {
360:                    compatible &= compatible(p1.getProxyServer(), p2
361:                            .getProxyServer());
362:                }
363:
364:                if (compatible) {
365:                    compatible &= !p1.isSetProxyServerPort()
366:                            || !p2.isSetProxyServerPort()
367:                            || p1.getProxyServerPort() == p2
368:                                    .getProxyServerPort();
369:                }
370:
371:                if (compatible) {
372:                    compatible &= !p1.isSetProxyServerType()
373:                            || !p2.isSetProxyServerType()
374:                            || p1.getProxyServerType().equals(
375:                                    p2.getProxyServerType());
376:                }
377:
378:                if (compatible) {
379:                    compatible &= compatible(p1.getReferer(), p2.getReferer());
380:                }
381:
382:                if (compatible) {
383:                    compatible &= p1.isAllowChunking() == p2.isAllowChunking();
384:                }
385:
386:                if (compatible) {
387:                    compatible &= p1.isAutoRedirect() == p2.isAutoRedirect();
388:                }
389:
390:                return compatible;
391:            }
392:
393:            /**
394:             * Returns a new HTTPClientPolicy that is compatible with the two specified policies or
395:             * null if no compatible policy can be determined.
396:             * @param p1 one policy
397:             * @param p2 another policy
398:             * @return the compatible policy
399:             */
400:            public static HTTPClientPolicy intersect(HTTPClientPolicy p1,
401:                    HTTPClientPolicy p2) {
402:
403:                // incompatibilities
404:
405:                if (!compatible(p1, p2)) {
406:                    return null;
407:                }
408:
409:                // ok - compute compatible policy
410:
411:                HTTPClientPolicy p = new HTTPClientPolicy();
412:                p.setAccept(combine(p1.getAccept(), p2.getAccept()));
413:                p.setAcceptEncoding(combine(p1.getAcceptEncoding(), p2
414:                        .getAcceptEncoding()));
415:                p.setAcceptLanguage(combine(p1.getAcceptLanguage(), p2
416:                        .getAcceptLanguage()));
417:                if (p1.isSetAllowChunking()) {
418:                    p.setAllowChunking(p1.isAllowChunking());
419:                } else if (p2.isSetAllowChunking()) {
420:                    p.setAllowChunking(p2.isAllowChunking());
421:                }
422:                if (p1.isSetAutoRedirect()) {
423:                    p.setAutoRedirect(p1.isAutoRedirect());
424:                } else if (p2.isSetAutoRedirect()) {
425:                    p.setAutoRedirect(p2.isAutoRedirect());
426:                }
427:                p.setAutoRedirect(p1.isAutoRedirect());
428:                p.setBrowserType(combine(p1.getBrowserType(), p2
429:                        .getBrowserType()));
430:                if (p1.isSetCacheControl()) {
431:                    p.setCacheControl(p1.getCacheControl());
432:                } else if (p2.isSetCacheControl()) {
433:                    p.setCacheControl(p2.getCacheControl());
434:                }
435:                if (p1.isSetConnection()) {
436:                    p.setConnection(p1.getConnection());
437:                } else if (p2.isSetConnection()) {
438:                    p.setConnection(p2.getConnection());
439:                }
440:                p.setContentType(p1.getContentType());
441:                p.setCookie(combine(p1.getCookie(), p2.getCookie()));
442:                p.setDecoupledEndpoint(combine(p1.getDecoupledEndpoint(), p2
443:                        .getDecoupledEndpoint()));
444:                p.setHost(combine(p1.getHost(), p2.getHost()));
445:                p.setProxyServer(combine(p1.getProxyServer(), p2
446:                        .getProxyServer()));
447:                if (p1.isSetProxyServerPort()) {
448:                    p.setProxyServerPort(p1.getProxyServerPort());
449:                } else if (p2.isSetProxyServerPort()) {
450:                    p.setProxyServerPort(p2.getProxyServerPort());
451:                }
452:                if (p1.isSetProxyServerType()) {
453:                    p.setProxyServerType(p1.getProxyServerType());
454:                } else if (p2.isSetProxyServerType()) {
455:                    p.setProxyServerType(p2.getProxyServerType());
456:                }
457:                p.setReferer(combine(p1.getReferer(), p2.getReferer()));
458:                if (p1.isSetConnectionTimeout() || p2.isSetConnectionTimeout()) {
459:                    p.setConnectionTimeout(Math.min(p1.getConnectionTimeout(),
460:                            p2.getConnectionTimeout()));
461:                }
462:                if (p1.isSetReceiveTimeout() || p2.isSetReceiveTimeout()) {
463:                    p.setReceiveTimeout(Math.min(p1.getReceiveTimeout(), p2
464:                            .getReceiveTimeout()));
465:                }
466:
467:                return p;
468:            }
469:
470:            /**
471:             * Determines if two HTTPClientPolicy objects are equal.
472:             * REVISIT: Check if this can be replaced by a generated equals method.
473:             * @param p1 one client policy
474:             * @param p2 another client policy
475:             * @return true iff the two policies are equal
476:             */
477:            public static boolean equals(HTTPClientPolicy p1,
478:                    HTTPClientPolicy p2) {
479:                if (p1 == p2) {
480:                    return true;
481:                }
482:                boolean result = true;
483:                result &= (p1.isAllowChunking() == p2.isAllowChunking())
484:                        && (p1.isAutoRedirect() == p2.isAutoRedirect())
485:                        && equals(p1.getAccept(), p2.getAccept())
486:                        && equals(p1.getAcceptEncoding(), p2
487:                                .getAcceptEncoding())
488:                        && equals(p1.getAcceptLanguage(), p2
489:                                .getAcceptLanguage())
490:                        && equals(p1.getBrowserType(), p2.getBrowserType());
491:                if (!result) {
492:                    return false;
493:                }
494:
495:                result &= (p1.getCacheControl() == null ? p2.getCacheControl() == null
496:                        : p1.getCacheControl().value().equals(
497:                                p2.getCacheControl().value())
498:                                && p1.getConnection().value().equals(
499:                                        p2.getConnection().value()))
500:                        && (p1.getConnectionTimeout() == p2
501:                                .getConnectionTimeout())
502:                        && equals(p1.getContentType(), p2.getContentType())
503:                        && equals(p1.getCookie(), p2.getCookie())
504:                        && equals(p1.getDecoupledEndpoint(), p2
505:                                .getDecoupledEndpoint())
506:                        && equals(p1.getHost(), p2.getHost());
507:                if (!result) {
508:                    return false;
509:                }
510:
511:                result &= equals(p1.getProxyServer(), p2.getProxyServer())
512:                        && (p1.isSetProxyServerPort() ? p1.getProxyServerPort() == p2
513:                                .getProxyServerPort()
514:                                : !p2.isSetProxyServerPort())
515:                        && p1.getProxyServerType().value().equals(
516:                                p2.getProxyServerType().value())
517:                        && (p1.getReceiveTimeout() == p2.getReceiveTimeout())
518:                        && equals(p1.getReferer(), p2.getReferer());
519:
520:                return result;
521:            }
522:
523:            /**
524:             * Checks if two HTTPServerPolicy objects are compatible.
525:             * @param p1 one server policy
526:             * @param p2 another server policy
527:             * @return true iff policies are compatible
528:             */
529:            public static boolean compatible(HTTPServerPolicy p1,
530:                    HTTPServerPolicy p2) {
531:
532:                if (p1 == p2 || p1.equals(p2)) {
533:                    return true;
534:                }
535:
536:                boolean compatible = true;
537:
538:                if (compatible) {
539:                    compatible &= !p1.isSetCacheControl()
540:                            || !p2.isSetCacheControl()
541:                            || p1.getCacheControl().value().equals(
542:                                    p2.getCacheControl().value());
543:                }
544:
545:                if (compatible) {
546:                    compatible &= compatible(p1.getContentEncoding(), p2
547:                            .getContentEncoding());
548:                }
549:
550:                if (compatible) {
551:                    compatible &= compatible(p1.getContentLocation(), p2
552:                            .getContentLocation());
553:                }
554:
555:                if (compatible) {
556:                    compatible &= p1.getContentType().equals(
557:                            p2.getContentType());
558:                }
559:
560:                if (compatible) {
561:                    compatible &= compatible(p1.getRedirectURL(), p2
562:                            .getRedirectURL());
563:                }
564:
565:                if (compatible) {
566:                    compatible &= compatible(p1.getServerType(), p2
567:                            .getServerType());
568:                }
569:
570:                if (compatible) {
571:                    compatible &= p1.isHonorKeepAlive() == p2
572:                            .isHonorKeepAlive();
573:                }
574:
575:                if (compatible) {
576:                    compatible &= p1.isSuppressClientReceiveErrors() == p2
577:                            .isSuppressClientReceiveErrors();
578:                }
579:
580:                if (compatible) {
581:                    compatible &= p1.isSuppressClientSendErrors() == p2
582:                            .isSuppressClientSendErrors();
583:                }
584:
585:                return compatible;
586:            }
587:
588:            /**
589:             * Returns a new HTTPServerPolicy that is compatible with the two specified policies or
590:             * null if no compatible policy can be determined.
591:             * @param p1 one policy
592:             * @param p2 another policy
593:             * @return the compatible policy
594:             */
595:            public static HTTPServerPolicy intersect(HTTPServerPolicy p1,
596:                    HTTPServerPolicy p2) {
597:
598:                if (!compatible(p1, p2)) {
599:                    return null;
600:                }
601:
602:                HTTPServerPolicy p = new HTTPServerPolicy();
603:                if (p1.isSetCacheControl()) {
604:                    p.setCacheControl(p1.getCacheControl());
605:                } else if (p2.isSetCacheControl()) {
606:                    p.setCacheControl(p2.getCacheControl());
607:                }
608:                p.setContentEncoding(combine(p1.getContentEncoding(), p2
609:                        .getContentEncoding()));
610:                p.setContentLocation(combine(p1.getContentLocation(), p2
611:                        .getContentLocation()));
612:                if (p1.isSetContentType()) {
613:                    p.setContentType(p1.getContentType());
614:                } else if (p2.isSetContentType()) {
615:                    p.setContentType(p2.getContentType());
616:                }
617:                if (p1.isSetHonorKeepAlive()) {
618:                    p.setHonorKeepAlive(p1.isHonorKeepAlive());
619:                } else if (p2.isSetHonorKeepAlive()) {
620:                    p.setHonorKeepAlive(p2.isHonorKeepAlive());
621:                }
622:                if (p1.isSetReceiveTimeout() || p2.isSetReceiveTimeout()) {
623:                    p.setReceiveTimeout(Math.min(p1.getReceiveTimeout(), p2
624:                            .getReceiveTimeout()));
625:                }
626:                p.setRedirectURL(combine(p1.getRedirectURL(), p2
627:                        .getRedirectURL()));
628:                p
629:                        .setServerType(combine(p1.getServerType(), p2
630:                                .getServerType()));
631:                if (p1.isSetSuppressClientReceiveErrors()) {
632:                    p.setSuppressClientReceiveErrors(p1
633:                            .isSuppressClientReceiveErrors());
634:                } else if (p2.isSetSuppressClientReceiveErrors()) {
635:                    p.setSuppressClientReceiveErrors(p2
636:                            .isSuppressClientReceiveErrors());
637:                }
638:                if (p1.isSetSuppressClientSendErrors()) {
639:                    p.setSuppressClientSendErrors(p1
640:                            .isSuppressClientSendErrors());
641:                } else if (p2.isSetSuppressClientSendErrors()) {
642:                    p.setSuppressClientSendErrors(p2
643:                            .isSuppressClientSendErrors());
644:                }
645:
646:                return p;
647:            }
648:
649:            /**
650:             * Determines if two HTTPServerPolicy objects are equal.
651:             * REVISIT: Check if this can be replaced by a generated equals method.
652:             * @param p1 one server policy
653:             * @param p2 another server policy
654:             * @return true iff the two policies are equal
655:             */
656:            public static boolean equals(HTTPServerPolicy p1,
657:                    HTTPServerPolicy p2) {
658:                if (p1 == p2) {
659:                    return true;
660:                }
661:                boolean result = true;
662:
663:                result &= (p1.isHonorKeepAlive() == p2.isHonorKeepAlive())
664:                        && (p1.getCacheControl() == null ? p2.getCacheControl() == null
665:                                : p1.getCacheControl().value().equals(
666:                                        p2.getCacheControl().value()))
667:                        && equals(p1.getContentEncoding(), p2
668:                                .getContentEncoding())
669:                        && equals(p1.getContentLocation(), p2
670:                                .getContentLocation())
671:                        && equals(p1.getContentType(), p2.getContentType());
672:                if (!result) {
673:                    return false;
674:                }
675:                result &= (p1.getReceiveTimeout() == p2.getReceiveTimeout())
676:                        && equals(p1.getRedirectURL(), p2.getRedirectURL())
677:                        && equals(p1.getServerType(), p2.getServerType())
678:                        && (p1.isSuppressClientReceiveErrors() == p2
679:                                .isSuppressClientReceiveErrors())
680:                        && (p1.isSuppressClientSendErrors() == p2
681:                                .isSuppressClientSendErrors());
682:
683:                return result;
684:            }
685:
686:            private static String combine(String s1, String s2) {
687:                return s1 == null ? s2 : s1;
688:            }
689:
690:            private static boolean equals(String s1, String s2) {
691:                return s1 == null ? s2 == null : s1.equals(s2);
692:            }
693:
694:            private static boolean compatible(String s1, String s2) {
695:                return s1 == null || s2 == null || s1.equals(s2);
696:            }
697:
698:            private static HTTPClientPolicy getClient(
699:                    Collection<Assertion> alternative) {
700:                HTTPClientPolicy compatible = null;
701:                for (Assertion a : alternative) {
702:                    if (HTTPCLIENTPOLICY_ASSERTION_QNAME.equals(a.getName())) {
703:                        HTTPClientPolicy p = JaxbAssertion.cast(a,
704:                                HTTPClientPolicy.class).getData();
705:                        if (null == compatible) {
706:                            compatible = p;
707:                        } else {
708:                            compatible = intersect(compatible, p);
709:                            if (null == compatible) {
710:                                LogUtils
711:                                        .log(LOG, Level.SEVERE,
712:                                                "INCOMPATIBLE_HTTPCLIENTPOLICY_ASSERTIONS");
713:                                org.apache.cxf.common.i18n.Message m = new org.apache.cxf.common.i18n.Message(
714:                                        "INCOMPATIBLE_HTTPCLIENTPOLICY_ASSERTIONS",
715:                                        LOG);
716:                                throw new PolicyException(m);
717:                            }
718:                        }
719:                    }
720:                }
721:                return compatible;
722:            }
723:
724:            private static HTTPServerPolicy getServer(
725:                    Collection<Assertion> alternative) {
726:                HTTPServerPolicy compatible = null;
727:                for (Assertion a : alternative) {
728:                    if (HTTPSERVERPOLICY_ASSERTION_QNAME.equals(a.getName())) {
729:                        HTTPServerPolicy p = JaxbAssertion.cast(a,
730:                                HTTPServerPolicy.class).getData();
731:                        if (null == compatible) {
732:                            compatible = p;
733:                        } else {
734:                            compatible = intersect(compatible, p);
735:                            if (null == compatible) {
736:                                LogUtils
737:                                        .log(LOG, Level.SEVERE,
738:                                                "INCOMPATIBLE_HTTPSERVERPOLICY_ASSERTIONS");
739:                                org.apache.cxf.common.i18n.Message m = new org.apache.cxf.common.i18n.Message(
740:                                        "INCOMPATIBLE_HTTPSERVERPOLICY_ASSERTIONS",
741:                                        LOG);
742:                                throw new PolicyException(m);
743:                            }
744:                        }
745:                    }
746:                }
747:                return compatible;
748:            }
749:
750:            public static String toString(HTTPClientPolicy p) {
751:                StringBuffer buf = new StringBuffer();
752:                buf.append(p);
753:                buf.append("[DecoupledEndpoint=\"");
754:                buf.append(p.getDecoupledEndpoint());
755:                buf.append("\", ReceiveTimeout=");
756:                buf.append(p.getReceiveTimeout());
757:                buf.append("])");
758:                return buf.toString();
759:            }
760:
761:            public static String toString(HTTPServerPolicy p) {
762:                StringBuffer buf = new StringBuffer();
763:                buf.append(p);
764:                buf.append("[ContentType=\"");
765:                buf.append(p.getContentType());
766:                buf.append("\", ReceiveTimeout=");
767:                buf.append(p.getReceiveTimeout());
768:                buf.append("])");
769:                return buf.toString();
770:
771:            }
772:
773:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.