1 5 6 package org.exoplatform.services.jcr.impl.core; 7 8 import java.io.ByteArrayInputStream ; 9 import java.io.IOException ; 10 import java.io.InputStream ; 11 import java.util.ArrayList ; 12 import java.util.Collection ; 13 import java.util.HashMap ; 14 import java.util.LinkedList ; 15 import java.util.Map ; 16 17 import javax.jcr.ActionVetoedException; 18 import javax.jcr.Credentials; 19 import javax.jcr.InvalidSerializedDataException; 20 import javax.jcr.ItemExistsException; 21 import javax.jcr.ItemNotFoundException; 22 import javax.jcr.LoginException; 23 import javax.jcr.NamespaceException; 24 import javax.jcr.Node; 25 import javax.jcr.PathNotFoundException; 26 import javax.jcr.Repository; 27 import javax.jcr.RepositoryException; 28 import javax.jcr.SimpleCredentials; 29 import javax.jcr.Ticket; 30 import javax.jcr.Workspace; 31 import javax.jcr.access.AccessDeniedException; 32 import javax.jcr.nodetype.ConstraintViolationException; 33 34 import org.exoplatform.container.PortalContainer; 35 import org.exoplatform.services.jcr.core.WorkspaceRegistry; 36 import org.exoplatform.services.jcr.impl.util.DocNodeImporter; 37 import org.exoplatform.services.jcr.impl.util.NodeTypeRecognizer; 38 import org.exoplatform.services.jcr.impl.util.SysNodeImporter; 39 import org.exoplatform.services.jcr.storage.WorkspaceContainer; 40 import org.xml.sax.ContentHandler ; 41 import org.xml.sax.SAXException ; 42 43 57 58 public class TicketImpl implements Ticket { 59 60 private static final String ANONYMOUS = "anonymous"; 61 63 private RepositoryImpl repository; 64 private Credentials credentials; 65 private Workspace workspace; 66 private NodesModificationManager nodesManager; 67 private String workspaceName; 68 private Map namespaces; 69 70 71 public TicketImpl(RepositoryImpl repository, Credentials credentials, String workspaceName) 72 throws RepositoryException { 73 74 WorkspaceRegistry workspaceRepository = (WorkspaceRegistry) PortalContainer.getInstance(). 75 getComponentInstanceOfType(WorkspaceRegistry.class); 76 this.repository = repository; 77 this.credentials = credentials; 78 if (credentials == null) { 79 this.credentials = new SimpleCredentials(ANONYMOUS, ANONYMOUS.toCharArray()); 80 } 81 this.workspaceName = workspaceName; 82 if (workspaceName == null) { 83 throw new RuntimeException ("Workspace name could not be NULL!"); 84 } 85 this.workspace = workspaceRepository.getWorkspace(repository.getName(), workspaceName, this); 86 this.nodesManager = new NodesModificationManager(this); 87 namespaces = new HashMap (); 88 } 89 90 94 public Repository getRepository() { 95 return repository; 96 } 97 98 103 public Credentials getCredentials() { 104 return credentials; 105 } 106 107 115 public Ticket impersonate(Credentials c) throws LoginException { 116 try { 117 return new TicketImpl(repository, c, workspaceName); 118 } catch (Exception e) { 119 throw new LoginException(e.getMessage()); 120 } 121 } 122 123 124 129 public Workspace getWorkspace() { 130 return workspace; 131 } 132 133 141 public Node getRootNode() throws RepositoryException { 142 NodeImpl node = (NodeImpl) nodesManager.getNodeByPath("/"); 143 if (node != null) { 144 node.setTicket(this); 145 } else 146 throw new RepositoryException("Ticket.getRootNode fatal error! RootNode could not be NULL"); 147 148 return node; 149 150 } 151 152 162 public Node getNodeByUUID(String uuid) throws ItemNotFoundException, RepositoryException { 163 NodeImpl node = (NodeImpl) nodesManager.getNodeByUUID(uuid); 164 if (node != null) { 165 node.setTicket(this); 166 } else 167 throw new ItemNotFoundException("Ticket.getNodeByUUID failed. Node with UUID='" + uuid + "' not found."); 168 169 return node; 170 } 171 172 180 public Node getNodeByAbsPath(String absPath) throws PathNotFoundException, RepositoryException { 181 NodeImpl node = (NodeImpl) nodesManager.getNodeByPath(absPath); 182 if (node != null) { 183 node.setTicket(this); 184 } else 185 throw new PathNotFoundException("Ticket.getNodeByAbsPath failed. Path'" + absPath + "' not found."); 186 187 return node; 188 } 189 190 191 223 public void save() throws AccessDeniedException, ConstraintViolationException, 224 ActionVetoedException, RepositoryException { 225 getRootNode().save(false); 226 } 227 228 234 public void revert() throws RepositoryException { 235 nodesManager.rollback((NodeImpl) getRootNode()); 236 } 237 238 274 public void importXML(String parentAbsPath, InputStream in) throws IOException , PathNotFoundException, 275 ItemExistsException, ConstraintViolationException, InvalidSerializedDataException, RepositoryException { 276 int type; 277 byte[] buffer = new byte[in.available()]; 278 ByteArrayInputStream str; 279 try { 280 in.read(buffer); 281 str = new ByteArrayInputStream (buffer); 282 type = NodeTypeRecognizer.recognize(str); 283 } catch (Exception e) { 284 throw new IOException (e.getMessage()); 285 } 286 287 str = new ByteArrayInputStream (buffer); 288 if (type == NodeTypeRecognizer.SYS){ 289 importSysView(parentAbsPath, str); 290 } else { 291 importDocView(parentAbsPath, str); 292 } 293 } 294 295 314 public ContentHandler importXML(String parentAbsPath) throws PathNotFoundException, ItemExistsException, RepositoryException { 315 throw new RuntimeException ("TODO implement importXML()!"); 316 } 317 318 332 public void setPrefix(String prefix, String uri) throws NamespaceException { 333 try { 334 workspace.getNamespaceRegistry().getPrefix(uri); namespaces.put(prefix, uri); 336 } catch (Exception e) { 337 throw new NamespaceException(e.getMessage()); 338 } 339 } 340 341 349 public String [] getPrefixes() { 350 Collection allPrefixes = new LinkedList (); 351 allPrefixes.addAll(namespaces.keySet()); 352 String [] permanentPrefixes = workspace.getNamespaceRegistry().getPrefixes(); 353 for (int i = 0; i < permanentPrefixes.length; i++) { 354 String permanentPrefix = permanentPrefixes[i]; 355 String uri = null; 356 try { 357 uri = workspace.getNamespaceRegistry().getURI(permanentPrefix); 358 if (!allPrefixes.contains(permanentPrefix) && !(namespaces.values().contains(uri))) { 359 allPrefixes.add(permanentPrefix); 360 } 361 } catch (RepositoryException e) { 362 e.printStackTrace(); 363 } 364 } 365 return (String []) allPrefixes.toArray(new String [allPrefixes.size()]); 366 } 367 368 376 public String getURI(String prefix) throws NamespaceException { 377 String uri = null; 378 try { 379 uri = (String ) namespaces.get(prefix); 381 if (uri != null) 382 return uri; 383 uri = workspace.getNamespaceRegistry().getURI(prefix); 384 if (namespaces.values().contains(uri)) 385 return null; 386 } catch (Exception e) { 387 throw new NamespaceException("Can not lookup the URI", e); 388 } 389 if (uri == null) 390 throw new NamespaceException("No such " + uri + " uri in the NamespaceRepository"); 391 392 return uri; 393 } 394 395 401 public void logout() { 402 } 403 404 405 private void importSysView(String parentPath, InputStream in) throws PathNotFoundException, 406 InvalidSerializedDataException, ConstraintViolationException, RepositoryException { 407 ArrayList items; 408 try { 409 SysNodeImporter.fillItems(parentPath, in, this); 410 } catch (IOException e) { 411 throw new InvalidSerializedDataException(e.getMessage(),e); 412 } catch (SAXException e) { 413 throw new InvalidSerializedDataException(e.getMessage(),e); 414 } 415 } 416 417 private ContentHandler importSysView(String parentPath) 418 throws InvalidSerializedDataException, ConstraintViolationException { 419 throw new ConstraintViolationException("Does Not implemented temporary!"); 420 } 421 422 private void importDocView(String parentPath, InputStream in) 423 throws PathNotFoundException, InvalidSerializedDataException, 424 ConstraintViolationException, RepositoryException { 425 try { 426 NodeImpl node = (NodeImpl) this.getNodeByAbsPath(parentPath); 427 node.setTicket(this); 428 DocNodeImporter.fillNode(node, in, new String [0]); 429 } catch (IOException e) { 430 throw new InvalidSerializedDataException("importDocView failed", e); 431 } catch (SAXException e) { 432 throw new InvalidSerializedDataException("importDocView failed", e); 433 } 434 } 435 436 private ContentHandler importDocView(String parentPath) 437 throws InvalidSerializedDataException, ConstraintViolationException { 438 throw new ConstraintViolationException("Does Not implemented temporary!"); 439 } 440 441 NodesModificationManager getNodesManager() { 442 return this.nodesManager; 443 } 444 445 public WorkspaceContainer getContainer() throws RepositoryException { 447 return repository.getContainer(workspaceName); 448 } 449 450 public String getWorkspaceName() { 451 return workspaceName; 452 } 453 454 } 455 | Popular Tags |