root / src / gr / ebs / gss / server / webdav / milton / GssOthersResource.java @ 9f4365c0
History | View | Annotate | Download (5.9 kB)
1 |
/*
|
---|---|
2 |
* Copyright 2011 Electronic Business Systems Ltd.
|
3 |
*
|
4 |
* This file is part of GSS.
|
5 |
*
|
6 |
* GSS is free software: you can redistribute it and/or modify
|
7 |
* it under the terms of the GNU General Public License as published by
|
8 |
* the Free Software Foundation, either version 3 of the License, or
|
9 |
* (at your option) any later version.
|
10 |
*
|
11 |
* GSS is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with GSS. If not, see <http://www.gnu.org/licenses/>.
|
18 |
*/
|
19 |
package gr.ebs.gss.server.webdav.milton; |
20 |
|
21 |
import static gr.ebs.gss.server.configuration.GSSConfigurationFactory.getConfiguration; |
22 |
import gr.ebs.gss.client.exceptions.ObjectNotFoundException; |
23 |
import gr.ebs.gss.client.exceptions.RpcException; |
24 |
import gr.ebs.gss.server.domain.User; |
25 |
import gr.ebs.gss.server.ejb.ExternalAPI; |
26 |
|
27 |
import java.io.IOException; |
28 |
import java.io.OutputStream; |
29 |
import java.util.ArrayList; |
30 |
import java.util.Date; |
31 |
import java.util.List; |
32 |
import java.util.Map; |
33 |
|
34 |
import javax.naming.Context; |
35 |
import javax.naming.InitialContext; |
36 |
import javax.naming.NamingException; |
37 |
import javax.rmi.PortableRemoteObject; |
38 |
|
39 |
import org.slf4j.Logger; |
40 |
import org.slf4j.LoggerFactory; |
41 |
|
42 |
import com.bradmcevoy.http.Auth; |
43 |
import com.bradmcevoy.http.CollectionResource; |
44 |
import com.bradmcevoy.http.DigestResource; |
45 |
import com.bradmcevoy.http.GetableResource; |
46 |
import com.bradmcevoy.http.HttpManager; |
47 |
import com.bradmcevoy.http.PropFindableResource; |
48 |
import com.bradmcevoy.http.Range; |
49 |
import com.bradmcevoy.http.Request; |
50 |
import com.bradmcevoy.http.Resource; |
51 |
import com.bradmcevoy.http.Request.Method; |
52 |
import com.bradmcevoy.http.exceptions.BadRequestException; |
53 |
import com.bradmcevoy.http.exceptions.NotAuthorizedException; |
54 |
import com.bradmcevoy.http.http11.auth.DigestResponse; |
55 |
|
56 |
|
57 |
/**
|
58 |
* @author kman
|
59 |
*
|
60 |
*/
|
61 |
public class GssOthersResource implements PropFindableResource, GetableResource, DigestResource, CollectionResource{ |
62 |
private static final Logger log = LoggerFactory.getLogger(GssOthersResource.class); |
63 |
String host;
|
64 |
GSSResourceFactory factory; |
65 |
User currentUser; |
66 |
|
67 |
/**
|
68 |
*
|
69 |
*/
|
70 |
public GssOthersResource(String host, GSSResourceFactory factory) { |
71 |
this.host=host;
|
72 |
this.factory=factory;
|
73 |
|
74 |
|
75 |
} |
76 |
|
77 |
@Override
|
78 |
public Date getCreateDate() { |
79 |
// TODO Auto-generated method stub
|
80 |
return null; |
81 |
} |
82 |
|
83 |
|
84 |
@Override
|
85 |
public String checkRedirect(Request arg0) { |
86 |
// TODO Auto-generated method stub
|
87 |
return null; |
88 |
} |
89 |
|
90 |
@Override
|
91 |
public Date getModifiedDate() { |
92 |
// TODO Auto-generated method stub
|
93 |
return null; |
94 |
} |
95 |
|
96 |
@Override
|
97 |
public String getName() { |
98 |
return "OthersShared"; |
99 |
} |
100 |
|
101 |
@Override
|
102 |
public String getUniqueId() { |
103 |
return "OthersShared"; |
104 |
} |
105 |
|
106 |
@Override
|
107 |
public Long getContentLength() { |
108 |
// TODO Auto-generated method stub
|
109 |
return null; |
110 |
} |
111 |
|
112 |
@Override
|
113 |
public String getContentType(String arg0) { |
114 |
// TODO Auto-generated method stub
|
115 |
return null; |
116 |
} |
117 |
|
118 |
@Override
|
119 |
public Long getMaxAgeSeconds(Auth arg0) { |
120 |
// TODO Auto-generated method stub
|
121 |
return null; |
122 |
} |
123 |
|
124 |
@Override
|
125 |
public void sendContent(OutputStream arg0, Range arg1, Map<String, String> arg2, String arg3) throws IOException, NotAuthorizedException, BadRequestException { |
126 |
// TODO Auto-generated method stub
|
127 |
|
128 |
} |
129 |
@Override
|
130 |
public Object authenticate(String user, String password) { |
131 |
return factory.getSecurityManager().authenticate(user, password);
|
132 |
} |
133 |
@Override
|
134 |
public Object authenticate( DigestResponse digestRequest ) { |
135 |
return factory.getSecurityManager().authenticate(digestRequest);
|
136 |
|
137 |
|
138 |
} |
139 |
|
140 |
@Override
|
141 |
public boolean authorise(Request request, Method method, Auth auth) { |
142 |
return factory.getSecurityManager().authorise(request, method, auth, this); |
143 |
} |
144 |
@Override
|
145 |
public String getRealm() { |
146 |
return factory.getRealm(this.host); |
147 |
} |
148 |
|
149 |
@Override
|
150 |
public boolean isDigestAllowed() { |
151 |
// TODO Auto-generated method stub
|
152 |
return true; |
153 |
} |
154 |
|
155 |
@Override
|
156 |
public Resource child(String arg0) { |
157 |
for(Resource r : getChildren()){
|
158 |
if(r.getName().equals(arg0))
|
159 |
return r;
|
160 |
} |
161 |
return null; |
162 |
} |
163 |
|
164 |
@Override
|
165 |
public List<? extends Resource> getChildren() { |
166 |
List<GssOtherUserResource> result = new ArrayList<GssOtherUserResource>(); |
167 |
List<User> users;
|
168 |
try {
|
169 |
users = getService().getUsersSharingFoldersForUser(getCurrentUser().getId()); |
170 |
log.info("USERS:"+users);
|
171 |
for(User u : users){
|
172 |
result.add(new GssOtherUserResource(host, factory, u));
|
173 |
} |
174 |
} catch (ObjectNotFoundException e) {
|
175 |
// TODO Auto-generated catch block
|
176 |
e.printStackTrace(); |
177 |
} catch (RpcException e) {
|
178 |
// TODO Auto-generated catch block
|
179 |
e.printStackTrace(); |
180 |
} |
181 |
|
182 |
|
183 |
return result;
|
184 |
} |
185 |
|
186 |
/**
|
187 |
* Retrieve the currentUser.
|
188 |
*
|
189 |
* @return the currentUser
|
190 |
*/
|
191 |
public User getCurrentUser() {
|
192 |
if(currentUser!=null) |
193 |
return currentUser;
|
194 |
String username = HttpManager.request().getHeaders().get("authorization"); |
195 |
if(username!=null){ |
196 |
username=GSSResourceFactory.getUsernameFromAuthHeader(username); |
197 |
try {
|
198 |
currentUser = getService().getUserByUserName(username); |
199 |
} catch (RpcException e) {
|
200 |
// TODO Auto-generated catch block
|
201 |
return null; |
202 |
} |
203 |
} |
204 |
return currentUser;
|
205 |
} |
206 |
|
207 |
/**
|
208 |
* A helper method that retrieves a reference to the ExternalAPI bean and
|
209 |
* stores it for future use.
|
210 |
*
|
211 |
* @return an ExternalAPI instance
|
212 |
* @throws RpcException in case an error occurs
|
213 |
*/
|
214 |
private ExternalAPI getService() throws RpcException { |
215 |
try {
|
216 |
final Context ctx = new InitialContext(); |
217 |
final Object ref = ctx.lookup(getConfiguration().getString("externalApiPath")); |
218 |
return (ExternalAPI) PortableRemoteObject.narrow(ref, ExternalAPI.class); |
219 |
} catch (final NamingException e) { |
220 |
log.error("Unable to retrieve the ExternalAPI EJB", e);
|
221 |
throw new RpcException("An error occurred while contacting the naming service"); |
222 |
} |
223 |
} |
224 |
|
225 |
} |