View Javadoc
1   /*
2    * Copyright 2024 Michael Osipov
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.michaelo.tomcat.pac;
17  
18  import java.nio.charset.StandardCharsets;
19  import java.util.Objects;
20  
21  /**
22   * A class representing the <a href=
23   * "https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-pac/e465cb27-4bc1-4173-8be0-b5fd64dc9ff7">{@code PAC_CLIENT_INFO}</a>
24   * structure from MS-PAC.
25   */
26  public class PacClientInfo {
27  
28  	private final String name;
29  
30  	/**
31  	 * Parses a PAC client info object from a byte array.
32  	 *
33  	 * @param infoBytes
34  	 *            PAC client info structure encoded as bytes
35  	 * @throws NullPointerException
36  	 *             if {@code infoBytes} is null
37  	 * @throws IllegalArgumentException
38  	 *             if {@code infoBytes} is empty
39  	 */
40  	public PacClientInfo(byte[] infoBytes) {
41  		Objects.requireNonNull(infoBytes, "infoBytes cannot be null");
42  		if (infoBytes.length == 0)
43  			throw new IllegalArgumentException("infoBytes cannot be empty");
44  
45  		PacDataBuffer buf = new PacDataBuffer(infoBytes);
46  
47  		// ClientId
48  		buf.skip(8);
49  		// NameLength
50  		int nameLength = buf.getUnsignedShort();
51  		// Name
52  		byte[] dst = new byte[nameLength];
53  		buf.get(dst);
54  		this.name = new String(dst, StandardCharsets.UTF_16LE);
55  	}
56  
57  	public String getName() {
58  		return name;
59  	}
60  
61  }