KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > protocol > v11 > InitializationRequirements


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.framework.protocol.v11;
20
21 import sync4j.framework.core.*;
22
23 import sync4j.framework.protocol.ProtocolException;
24 import sync4j.framework.protocol.v11.BasicRequirements;
25 import sync4j.framework.protocol.v11.Errors;
26
27 /**
28  * This class groups utility methods used for checking that an initialization
29  * massage follows the requirements that the protocol mandates.
30  *
31  * @author Stefano Fornari @ Funambol
32  *
33  * @version $Id: InitializationRequirements.java,v 1.5 2005/03/02 20:57:38 harrie Exp $
34  */

35 public class InitializationRequirements
36 extends BasicRequirements
37 implements Errors {
38     
39     // --------------------------------------------------------------- Constants
40

41     // ---------------------------------------------------------- Public methods
42

43     static public void checkAlertCommand(Alert alert) throws ProtocolException {
44         try {
45             BasicRequirements.checkAlertCommand(alert);
46         } catch (ProtocolException e) {
47             String JavaDoc[] args = new String JavaDoc[] { e.getMessage() };
48             throw new ProtocolException(ERRMSG_INVALID_ALERT, args);
49         }
50
51         //
52
// Checks the alert code
53
//
54
if (!AlertCode.isInitializationCode(alert.getData())) {
55             String JavaDoc[] args = new String JavaDoc[] { String.valueOf(alert.getData()) };
56             throw new ProtocolException(ERRMSG_INVALID_ALERT_CODE, args);
57         }
58
59         //
60
// Checks source and target
61
//
62
Item[] items = (Item[])alert.getItems().toArray(new Item[0]);
63
64         if ((items == null) || (items.length ==0)) {
65             throw new ProtocolException(ERRMSG_MISSING_ITEM);
66         }
67
68         try {
69             checkSource(items[0].getSource());
70         } catch (ProtocolException e) {
71             String JavaDoc[] args = new String JavaDoc[] { "source" };
72             throw new ProtocolException(ERRMSG_INVALID_ALERT, args);
73         }
74
75         try {
76             checkTarget(items[0].getTarget());
77         } catch (ProtocolException e) {
78             String JavaDoc[] args = new String JavaDoc[] { "target" };
79             throw new ProtocolException(ERRMSG_INVALID_ALERT, args);
80         }
81
82         //
83
// Checks sync anchors
84
//
85
Meta meta = items[0].getMeta();
86        
87         if (meta == null || meta.getAnchor() == null) {
88            throw new ProtocolException(ERRMSG_MISSING_SYNC_ANCHOR);
89         }
90     }
91     
92     /**
93      * Checks if the given command contains a valid request for server capablities.
94      *
95      * @param cmd the command containing the request
96      *
97      * @throws ProtocolException
98      */

99     static public void checkCapabilitiesRequest(Get cmd)
100     throws ProtocolException {
101         //
102
// Checks command id
103
//
104
try {
105             checkCommandId(cmd.getCmdID());
106         } catch (ProtocolException e) {
107             String JavaDoc[] args = new String JavaDoc[] { e.getMessage() };
108             throw new ProtocolException(ERRMSG_INVALID_CAPABILITIES, args);
109         }
110         
111         Item[] items = (Item[])cmd.getItems().toArray(new Item[0]);
112
113         if ((items == null) || (items.length ==0)) {
114             String JavaDoc[] args = new String JavaDoc[] {ERRMSG_MISSING_ITEM};
115             throw new ProtocolException(ERRMSG_INVALID_CAPABILITIES_REQUEST, args);
116         }
117
118         try {
119             checkTarget(items[0].getTarget());
120         } catch (ProtocolException e) {
121             String JavaDoc[] args = new String JavaDoc[] { "missing target" };
122             throw new ProtocolException(ERRMSG_INVALID_CAPABILITIES_REQUEST, args);
123         }
124     }
125 }
Popular Tags