1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.metricshub.wbem.sblim.cimclient.internal.http;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 import java.io.BufferedReader;
48 import java.io.File;
49 import java.io.FileInputStream;
50 import java.io.FileReader;
51 import java.io.IOException;
52 import java.io.InputStreamReader;
53 import java.net.URI;
54 import java.util.logging.Level;
55 import org.metricshub.wbem.sblim.cimclient.internal.logging.LogAndTraceBroker;
56 import org.metricshub.wbem.sblim.cimclient.internal.logging.Messages;
57 import org.metricshub.wbem.sblim.cimclient.internal.util.WBEMConstants;
58
59
60
61
62 public class PegasusLocalAuthInfo extends AuthorizationInfo {
63 private boolean iChallenged = false;
64
65
66
67
68 public PegasusLocalAuthInfo() {
69 super();
70 }
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 @Override
87 public void updateAuthenticationInfo(Challenge challenge, String authenticate, URI url, String requestMethod) {
88 this.iChallenged = true;
89 this.iResponse = authenticate;
90 }
91
92
93
94
95
96
97 @Override
98 public String toString() {
99 if (this.iChallenged && this.iResponse != null && this.iResponse.startsWith("Local ")) {
100 String fileName = "";
101 BufferedReader in = null;
102
103 try {
104 fileName = this.iResponse.substring(7, this.iResponse.length() - 1);
105
106 if (fileName.length() == 0) throw new IOException("No local authorization file specified");
107
108 File authorizationFile = new File(fileName);
109
110 if (!authorizationFile.canRead()) throw new IOException("Local authorization file not accessible");
111
112 in =
113 WBEMConstants.Z_OS.equals(System.getProperty(WBEMConstants.OS_NAME))
114 ? new BufferedReader(
115 new InputStreamReader(new FileInputStream(authorizationFile), WBEMConstants.ISO_8859_1)
116 )
117 : new BufferedReader(new FileReader(authorizationFile));
118
119 StringBuffer buffer = new StringBuffer();
120 String line;
121
122 while (true) {
123 line = in.readLine();
124 if (line == null) break;
125 buffer.append(line);
126 }
127
128 StringBuffer header = new StringBuffer();
129 header.append("Local \"");
130 header.append(getCredentials().getUserName());
131 header.append(':');
132 header.append(fileName);
133 header.append(':');
134 header.append(buffer);
135 header.append('"');
136
137 return header.toString();
138 } catch (IOException e) {
139 LogAndTraceBroker logger = LogAndTraceBroker.getBroker();
140 logger.trace(Level.FINER, "Exception while reading OpenPegasus local authorization file", e);
141 logger.message(Messages.HTTP_PEGASUS_LOCAL_AUTH_READ, fileName);
142 } finally {
143 if (in != null) {
144 try {
145 in.close();
146 } catch (IOException e) {
147 LogAndTraceBroker
148 .getBroker()
149 .trace(Level.FINER, "Exception while closing OpenPegasus local authorization file", e);
150 }
151 }
152 }
153 }
154
155 return "Local \"" + getCredentials().getUserName() + "\"";
156 }
157
158 @Override
159 public String getHeaderFieldName() {
160 return "PegasusAuthorization";
161 }
162
163 @Override
164 public boolean isSentOnFirstRequest() {
165 return true;
166 }
167
168 @Override
169 public boolean isKeptAlive() {
170 return true;
171 }
172 }