/
WebdavUrlConnection.java
183 lines (163 loc) · 5.43 KB
/
WebdavUrlConnection.java
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package com.oxygenxml.examples.webdav;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import org.apache.commons.io.IOUtils;
import com.google.common.io.Closeables;
import lombok.extern.slf4j.Slf4j;
import ro.sync.basic.util.URLUtil;
import ro.sync.ecss.extensions.api.webapp.WebappMessage;
import ro.sync.ecss.extensions.api.webapp.access.WebappPluginWorkspace;
import ro.sync.ecss.extensions.api.webapp.plugin.FilterURLConnection;
import ro.sync.ecss.extensions.api.webapp.plugin.UserActionRequiredException;
import ro.sync.exml.plugin.urlstreamhandler.CacheableUrlConnection;
import ro.sync.exml.workspace.api.PluginResourceBundle;
import ro.sync.exml.workspace.api.PluginWorkspaceProvider;
import ro.sync.net.protocol.FolderEntryDescriptor;
import ro.sync.net.protocol.http.WebdavLockHelper;
/**
* Wrapper over an URLConnection that reports 401 exceptions as
* {@link UserActionRequiredException}.
*
* @author cristi_talau
*/
@Slf4j
public class WebdavUrlConnection extends FilterURLConnection
implements CacheableUrlConnection {
/**
* The session ID.
*/
private String contextId;
/**
* Constructor method for the URLConnection wrapper.
* @param contextId The session ID.
*
* @param delegate the wrapped URLConnection.
*/
protected WebdavUrlConnection(String contextId, URLConnection delegate) {
super(delegate);
this.contextId = contextId;
}
@Override
public void connect() throws IOException {
try {
super.connect();
} catch (IOException e) {
handleException(e);
}
}
@Override
public InputStream getInputStream() throws IOException {
try {
return super.getInputStream();
} catch (IOException e) {
handleException(e);
// Unreachable.
return null;
}
}
@Override
public OutputStream getOutputStream() throws IOException {
// Before trying to save a resource, add the lock header if we have one.
new WebdavLockHelper().addLockHeader(
this.contextId, (HttpURLConnection) delegateConnection);
try {
return new FilterOutputStream(super.getOutputStream()) {
@Override
public void close() throws IOException {
try {
super.close();
URLUtil.disconnect(delegateConnection);
} catch (IOException e) {
handleException(e);
}
}
};
} catch (IOException e) {
handleException(e);
// Unreachable.
return null;
}
}
/**
* Filters the exceptions.
*
* @param e the exception to filter.
*
* @throws UserActionRequiredException if the exception message contains a 401 status.
*
* @throws IOException the param exception if it does not contain a 401 status.
*/
private void handleException(IOException e) throws UserActionRequiredException, IOException {
if (e.getMessage().indexOf("401") != -1) {
// log failed login attempts.
URL url = this.delegateConnection.getURL();
String userInfo = url.getUserInfo();
if(userInfo != null && !userInfo.isEmpty()) {
String user = URLUtil.extractUser(userInfo);
String password = URLUtil.extractPassword(userInfo);
if (user != null && !user.trim().isEmpty() && password != null && !password.trim().isEmpty()) {
log.warn("Failed login attempt of user " + user + " for " + URLUtil.getDescription(url));
}
}
log.debug("WebDAV not authorized exception " + e.getMessage());
PluginResourceBundle rb = ((WebappPluginWorkspace)PluginWorkspaceProvider.getPluginWorkspace()).getResourceBundle();
throw new UserActionRequiredException(new WebappMessage(
WebappMessage.MESSAGE_TYPE_CUSTOM,
rb.getMessage(TranslationTags.AUTHENTICATION_REQUIRED),
// send back the URL for which to authenticate.
this.delegateConnection.getURL().toExternalForm(),
true));
} else {
if (delegateConnection instanceof HttpURLConnection) {
String serverMessage = null;
InputStream errorStream = null;
try {
errorStream = ((HttpURLConnection) this.delegateConnection).getErrorStream();
serverMessage = IOUtils.toString(errorStream);
} catch (Exception ex) {
Closeables.closeQuietly(errorStream);
}
if (shouldDisplayServerMessage(serverMessage)) {
throw new IOException(serverMessage, e);
}
}
}
throw e;
}
@Override
public List<FolderEntryDescriptor> listFolder() throws IOException {
try {
return super.listFolder();
} catch(IOException e) {
handleException(e);
// Unreachable
return null;
}
}
/**
* Decide whether to display the message returned by the WebDAV server.
*
* @param serverMessage The server message.
*
* @return <code>true</code> if we should display the server message.
*/
private boolean shouldDisplayServerMessage(String serverMessage) {
if (serverMessage == null) {
return false;
}
if (serverMessage.contains("<body") || serverMessage.contains("</body")) {
return false;
}
// box.com returns an XML description of the error - do not show that description.
if (serverMessage.contains("<?xml")) {
return false;
}
return true;
}
}