KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > clientimpl > acl > RemoteAclStrategy


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
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 org.outerj.daisy.repository.clientimpl.acl;
17
18 import org.outerj.daisy.repository.commonimpl.acl.AclStrategy;
19 import org.outerj.daisy.repository.commonimpl.acl.AclImpl;
20 import org.outerj.daisy.repository.commonimpl.AuthenticatedUser;
21 import org.outerj.daisy.repository.Document;
22 import org.outerj.daisy.repository.RepositoryException;
23 import org.outerj.daisy.repository.VariantKey;
24 import org.outerj.daisy.repository.VariantKeys;
25 import org.outerj.daisy.repository.clientimpl.infrastructure.AbstractRemoteStrategy;
26 import org.outerj.daisy.repository.clientimpl.infrastructure.DaisyHttpClient;
27 import org.outerj.daisy.repository.clientimpl.RemoteRepositoryManager;
28 import org.outerj.daisy.repository.acl.*;
29 import org.outerj.daisy.repository.acl.AclPermission;
30 import org.apache.commons.httpclient.HttpMethod;
31 import org.apache.commons.httpclient.NameValuePair;
32 import org.apache.commons.httpclient.methods.GetMethod;
33 import org.apache.commons.httpclient.methods.PostMethod;
34 import org.outerx.daisy.x10.*;
35
36 import java.util.ArrayList JavaDoc;
37 import java.util.List JavaDoc;
38
39 public class RemoteAclStrategy extends AbstractRemoteStrategy implements AclStrategy {
40     public RemoteAclStrategy(RemoteRepositoryManager.Context context) {
41         super(context);
42     }
43
44     public AclImpl loadAcl(long id, AuthenticatedUser user) throws RepositoryException {
45         DaisyHttpClient httpClient = getClient(user);
46
47         aclIdCheck(id);
48         HttpMethod method = new GetMethod("/repository/acl/" + getAclName(id));
49
50         AclDocument aclDocument = (AclDocument)httpClient.executeMethod(method, AclDocument.class, true);
51         AclDocument.Acl aclXml = aclDocument.getAcl();
52         AclImpl document = new AclImpl(this, aclXml.getLastModified().getTime(), aclXml.getLastModifier(), aclXml.getId(), user, aclXml.getUpdateCount());
53         document.setFromXml(aclXml);
54         return document;
55     }
56
57     private void aclIdCheck(long id) throws RepositoryException {
58         if (id != 1 && id != 2)
59             throw new RepositoryException("Invalid ACL ID: " + id);
60     }
61
62     private String JavaDoc getAclName(long id) {
63         if (id == 1)
64             return "live";
65         else if (id == 2)
66             return "staging";
67         else
68             throw new RuntimeException JavaDoc("Unsupported ACL ID: " + id);
69     }
70
71     public void storeAcl(AclImpl acl) throws RepositoryException {
72         AclImpl.IntimateAccess aclInt = acl.getIntimateAccess(this);
73         DaisyHttpClient httpClient = getClient(aclInt.getCurrentModifier());
74         PostMethod method = new PostMethod("/repository/acl/" + getAclName(aclInt.getId()));
75
76         AclDocument aclDocument = acl.getXml();
77         method.setRequestBody(aclDocument.newInputStream());
78
79         aclDocument = (AclDocument)httpClient.executeMethod(method, AclDocument.class, true);
80         AclDocument.Acl aclXml = aclDocument.getAcl();
81         aclInt.setLastModified(aclXml.getLastModified().getTime());
82         aclInt.setLastModifier(aclXml.getLastModifier());
83         aclInt.setUpdateCount(aclXml.getUpdateCount());
84     }
85
86     public void copyStagingToLive(AuthenticatedUser user) throws RepositoryException {
87         DaisyHttpClient httpClient = getClient(user);
88
89         HttpMethod method = new GetMethod("/repository/acl/staging?action=putLive");
90         httpClient.executeMethod(method, null, true);
91     }
92
93     public void copyLiveToStaging(AuthenticatedUser user) throws RepositoryException {
94         DaisyHttpClient httpClient = getClient(user);
95
96         HttpMethod method = new GetMethod("/repository/acl/staging?action=revertChanges");
97         httpClient.executeMethod(method, null, true);
98     }
99
100     public AclResultInfo getAclInfo(AuthenticatedUser user, long id, long userId, long[] roleIds, Document document) throws RepositoryException {
101         throw new RuntimeException JavaDoc("This operation is not supported in the remote API implementation.");
102     }
103
104     public AclResultInfo getAclInfo(AuthenticatedUser user, long id, long userId, long[] roleIds, long documentId, long branchId, long languageId) throws RepositoryException {
105         DaisyHttpClient httpClient = getClient(user);
106
107         aclIdCheck(id);
108         HttpMethod method = new GetMethod("/repository/acl/" + getAclName(id));
109         List JavaDoc parameters = new ArrayList JavaDoc();
110         parameters.add(new NameValuePair("action", "evaluate"));
111         parameters.add(new NameValuePair("documentId", String.valueOf(documentId)));
112         parameters.add(new NameValuePair("branch", String.valueOf(branchId)));
113         parameters.add(new NameValuePair("language", String.valueOf(languageId)));
114         parameters.add(new NameValuePair("user", String.valueOf(userId)));
115         for(int i = 0; i < roleIds.length; i++)
116             parameters.add(new NameValuePair("role", String.valueOf(roleIds[i])));
117         method.setQueryString((NameValuePair[])parameters.toArray(new NameValuePair[parameters.size()]));
118
119         AclResultDocument aclResultDocument = (AclResultDocument)httpClient.executeMethod(method, AclResultDocument.class, true);
120         AclResultDocument.AclResult aclResultXml = aclResultDocument.getAclResult();
121         AclResultInfo aclResultInfo = new AclResultInfo(aclResultXml.getUser().getId(), aclResultXml.getUser().getRoles().getRoleIdArray(), aclResultXml.getDocumentId(), aclResultXml.getBranchId(), aclResultXml.getLanguageId());
122         aclResultInfo.setFromXml(aclResultXml);
123         return aclResultInfo;
124     }
125
126     public long[] filterDocumentTypes(AuthenticatedUser user, long[] documentTypeIds, long collectionId) throws RepositoryException {
127         DaisyHttpClient httpClient = getClient(user);
128         PostMethod method = new PostMethod("/repository/filterDocumentTypes");
129
130         NameValuePair[] parameters = new NameValuePair[1];
131         parameters[0] = new NameValuePair("collectionId", String.valueOf(collectionId));
132         method.setQueryString(parameters);
133         
134         IdsDocument idsDocument = IdsDocument.Factory.newInstance();
135         idsDocument.addNewIds().setIdArray(documentTypeIds);
136         method.setRequestBody(idsDocument.newInputStream());
137
138         idsDocument = (IdsDocument)httpClient.executeMethod(method, IdsDocument.class, true);
139         return idsDocument.getIds().getIdArray();
140     }
141
142     public VariantKey[] filterDocuments(AuthenticatedUser user, VariantKey[] variantKeys, AclPermission permission) throws RepositoryException {
143         DaisyHttpClient httpClient = getClient(user);
144         PostMethod method = new PostMethod("/repository/filterDocuments");
145         method.setQueryString(new NameValuePair[] {new NameValuePair("permission", permission.toString())});
146         method.setRequestBody(new VariantKeys(variantKeys).getXml().newInputStream());
147         VariantKeysDocument variantKeysDocument = (VariantKeysDocument)httpClient.executeMethod(method, VariantKeysDocument.class, true);
148         return VariantKeys.fromXml(variantKeysDocument).getArray();
149     }
150 }
151
Popular Tags