]> git.argeo.org Git - gpl/argeo-slc.git/blob - plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/DistUiUtils.java
Add password spec type
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / dist / DistUiUtils.java
1 package org.argeo.slc.client.ui.dist;
2
3 import javax.jcr.Credentials;
4 import javax.jcr.GuestCredentials;
5 import javax.jcr.Node;
6 import javax.jcr.NodeIterator;
7 import javax.jcr.Property;
8 import javax.jcr.PropertyIterator;
9 import javax.jcr.Repository;
10 import javax.jcr.RepositoryException;
11 import javax.jcr.RepositoryFactory;
12 import javax.jcr.SimpleCredentials;
13 import javax.jcr.nodetype.NodeType;
14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17 import org.argeo.ArgeoException;
18 import org.argeo.jcr.ArgeoJcrUtils;
19 import org.argeo.jcr.ArgeoNames;
20 import org.argeo.jcr.ArgeoTypes;
21 import org.argeo.jcr.JcrUtils;
22 import org.argeo.slc.SlcException;
23 import org.argeo.slc.jcr.SlcNames;
24 import org.argeo.slc.jcr.SlcTypes;
25 import org.argeo.util.security.Keyring;
26
27 /** Static utilities */
28 public class DistUiUtils implements ArgeoNames, SlcNames {
29 private final static Log log = LogFactory.getLog(DistUiUtils.class);
30
31 /** Retrieve repository based on information in the repo node */
32 public static Repository getRepository(RepositoryFactory repositoryFactory,
33 Keyring keyring, Node repoNode) {
34 try {
35 Repository repository;
36 if (repoNode.isNodeType(ArgeoTypes.ARGEO_REMOTE_REPOSITORY)) {
37 String uri = repoNode.getProperty(ARGEO_URI).getString();
38 if (uri.startsWith("http")) {// http, https
39 repository = ArgeoJcrUtils.getRepositoryByUri(
40 repositoryFactory, uri);
41 } else {// alias
42 String alias = uri;
43 repository = ArgeoJcrUtils.getRepositoryByAlias(
44 repositoryFactory, alias);
45 }
46 return repository;
47 } else {
48 throw new SlcException("Unsupported node type " + repoNode);
49 }
50 } catch (RepositoryException e) {
51 throw new SlcException("Cannot connect to repository " + repoNode,
52 e);
53 }
54
55 }
56
57 /**
58 * Reads credentials from node, using keyring if there is a password. Cann
59 * return null if no credentials needed (local repo) at all, but returns
60 * {@link GuestCredentials} if user id is 'anonymous' .
61 */
62 public static Credentials getRepositoryCredentials(Keyring keyring,
63 Node repoNode) {
64 try {
65 if (repoNode.isNodeType(ArgeoTypes.ARGEO_REMOTE_REPOSITORY)) {
66 if (!repoNode.hasProperty(ARGEO_USER_ID))
67 return null;
68
69 String userId = repoNode.getProperty(ARGEO_USER_ID).getString();
70 if (userId.equals("anonymous"))// FIXME hardcoded userId
71 return new GuestCredentials();
72 char[] password = keyring.getAsChars(repoNode.getPath() + '/'
73 + ARGEO_PASSWORD);
74 Credentials credentials = new SimpleCredentials(userId,
75 password);
76 return credentials;
77 } else {
78 throw new SlcException("Unsupported node type " + repoNode);
79 }
80 } catch (RepositoryException e) {
81 throw new SlcException("Cannot connect to repository " + repoNode,
82 e);
83 }
84 }
85
86 /**
87 * Custom copy since the one in commons does not fit the needs when copying
88 * a workspace completely.
89 */
90 public static void copy(Node fromNode, Node toNode) {
91 try {
92 if (log.isDebugEnabled())
93 log.debug("copy node :" + fromNode.getPath());
94
95 // FIXME : small hack to enable specific workspace copy
96 if (fromNode.isNodeType("rep:ACL")
97 || fromNode.isNodeType("rep:system")) {
98 if (log.isTraceEnabled())
99 log.trace("node " + fromNode + " skipped");
100 return;
101 }
102
103 // add mixins
104 for (NodeType mixinType : fromNode.getMixinNodeTypes()) {
105 toNode.addMixin(mixinType.getName());
106 }
107
108 // Double check
109 for (NodeType mixinType : toNode.getMixinNodeTypes()) {
110 if (log.isDebugEnabled())
111 log.debug(mixinType.getName());
112 }
113
114 // process properties
115 PropertyIterator pit = fromNode.getProperties();
116 properties: while (pit.hasNext()) {
117 Property fromProperty = pit.nextProperty();
118 String propName = fromProperty.getName();
119 try {
120 String propertyName = fromProperty.getName();
121 if (toNode.hasProperty(propertyName)
122 && toNode.getProperty(propertyName).getDefinition()
123 .isProtected())
124 continue properties;
125
126 if (fromProperty.getDefinition().isProtected())
127 continue properties;
128
129 if (propertyName.equals("jcr:created")
130 || propertyName.equals("jcr:createdBy")
131 || propertyName.equals("jcr:lastModified")
132 || propertyName.equals("jcr:lastModifiedBy"))
133 continue properties;
134
135 if (fromProperty.isMultiple()) {
136 toNode.setProperty(propertyName,
137 fromProperty.getValues());
138 } else {
139 toNode.setProperty(propertyName,
140 fromProperty.getValue());
141 }
142 } catch (RepositoryException e) {
143 throw new ArgeoException("Cannot property " + propName, e);
144 }
145 }
146
147 // recursively process children nodes
148 NodeIterator nit = fromNode.getNodes();
149 while (nit.hasNext()) {
150 Node fromChild = nit.nextNode();
151 Integer index = fromChild.getIndex();
152 String nodeRelPath = fromChild.getName() + "[" + index + "]";
153 Node toChild;
154 if (toNode.hasNode(nodeRelPath))
155 toChild = toNode.getNode(nodeRelPath);
156 else
157 toChild = toNode.addNode(fromChild.getName(), fromChild
158 .getPrimaryNodeType().getName());
159 copy(fromChild, toChild);
160 }
161
162 // update jcr:lastModified and jcr:lastModifiedBy in toNode in
163 // case
164 // they existed
165 if (!toNode.getDefinition().isProtected()
166 && toNode.isNodeType(NodeType.MIX_LAST_MODIFIED))
167 JcrUtils.updateLastModified(toNode);
168
169 // Workaround to reduce session size: artifact is a saveable
170 // unity
171 if (toNode.isNodeType(SlcTypes.SLC_ARTIFACT))
172 toNode.getSession().save();
173
174 } catch (RepositoryException e) {
175 throw new ArgeoException("Cannot copy " + fromNode + " to "
176 + toNode, e);
177 }
178 }
179
180 private DistUiUtils() {
181
182 }
183 }