KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > server > auth > ntlm > Type1NTLMMessage


1 /*
2  * Copyright (C) 2006 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.server.auth.ntlm;
18
19 /**
20  * Type1 NTLM Message Class
21  *
22  * @author GKSpencer
23  */

24 public class Type1NTLMMessage extends NTLMMessage
25 {
26     // Minimal type 1 message length
27

28     public static final int MinimalMessageLength = 16;
29     
30     // Type 1 field offsets
31

32     public static final int OffsetFlags = 12;
33     public static final int OffsetData = 16;
34     
35     /**
36      * Default constructor
37      */

38     public Type1NTLMMessage()
39     {
40         super();
41     }
42     
43     /**
44      * Class constructor
45      *
46      * @param buf byte[]
47      */

48     public Type1NTLMMessage(byte[] buf)
49     {
50         super(buf, 0, buf.length);
51     }
52     
53     /**
54      * Class constructor
55      *
56      * @param buf byte[]
57      * @param offset int
58      * @param len int
59      */

60     public Type1NTLMMessage(byte[] buf, int offset, int len)
61     {
62         super(buf, offset, len);
63     }
64
65     /**
66      * Return the flags value
67      *
68      * @return int
69      */

70     public int getFlags()
71     {
72         return getIntValue(OffsetFlags);
73     }
74     
75     /**
76      * Check if the domain security buffer is included
77      *
78      * @return boolean
79      */

80     public final boolean hasDomain()
81     {
82         if ( getLength() == MinimalMessageLength || hasFlag(NTLM.FlagDomainSupplied) == false)
83             return false;
84         return true;
85     }
86     
87     /**
88      * Return the domain name
89      *
90      * @return String
91      */

92     public final String JavaDoc getDomain()
93     {
94         if ( hasFlag(NTLM.FlagDomainSupplied) == false)
95             return null;
96         
97         return getStringValue(OffsetData, false);
98     }
99     
100     /**
101      * Check if the workstation security buffer is included
102      *
103      * @return boolean
104      */

105     public final boolean hasWorkstation()
106     {
107         if ( getLength() == MinimalMessageLength || hasFlag(NTLM.FlagWorkstationSupplied) == false)
108             return false;
109         return true;
110     }
111     
112     /**
113      * Return the workstation name
114      *
115      * @return String
116      */

117     public final String JavaDoc getWorkstation()
118     {
119         if ( hasFlag(NTLM.FlagWorkstationSupplied) == false)
120             return null;
121         
122         int bufPos = OffsetData;
123         if ( hasFlag(NTLM.FlagDomainSupplied))
124             bufPos += BufferHeaderLen;
125             
126         return getStringValue(bufPos, false);
127     }
128     
129     /**
130      * Build a type 1 message
131      *
132      * @param flags int
133      * @param domain String
134      * @param workstation String
135      */

136     public final void buildType1(int flags, String JavaDoc domain, String JavaDoc workstation)
137     {
138         int bufPos = OffsetData;
139         int strOff = OffsetData;
140         
141         if ( domain != null)
142             strOff += BufferHeaderLen;
143         if ( workstation != null)
144             strOff += BufferHeaderLen;
145
146         // Pack the domain name
147

148         if ( domain != null)
149         {
150             strOff = setStringValue(bufPos, domain, strOff, false);
151             flags |= NTLM.FlagDomainSupplied;
152             bufPos += BufferHeaderLen;
153         }
154
155         // Pack the workstation name
156

157         if ( workstation != null)
158         {
159             strOff = setStringValue(bufPos, workstation, strOff, false);
160             flags |= NTLM.FlagWorkstationSupplied;
161         }
162
163         // Initialize the header/flags
164

165         initializeHeader(NTLM.Type1, flags);
166         
167         // Set the message length
168

169         setLength(strOff);
170     }
171     
172     /**
173      * Set the message flags
174      *
175      * @param flags int
176      */

177     protected void setFlags(int flags)
178     {
179         setIntValue( OffsetFlags, flags);
180     }
181     
182     /**
183      * Return the type 1 message as a string
184      *
185      * @return String
186      */

187     public String JavaDoc toString()
188     {
189         StringBuilder JavaDoc str = new StringBuilder JavaDoc();
190         
191         str.append("[Type1:0x");
192         str.append(Integer.toHexString(getFlags()));
193         str.append(",Domain:");
194         if ( hasDomain())
195             str.append(getDomain());
196         else
197             str.append("<NotSet>");
198         str.append(",Wks:");
199         
200         if ( hasWorkstation())
201             str.append(getWorkstation());
202         else
203             str.append("<NotSet>");
204         str.append("]");
205         
206         return str.toString();
207     }
208 }
209
Popular Tags