Package org.persvr.data

Examples of org.persvr.data.Persistable


            String changesSinceString = PersevereFilter.getParameter(httpRequest,"changesSince");
            if (changesSinceString != null)
              ifModifiedSince = Long.parseLong(changesSinceString);
            locale = PersevereFilter.getHeader(httpRequest,"Accept-Language");
          }
          Persistable object = null;
              //Id requestedId = new Id(requestedObject); // removes the leading underscore
              //requestedId.source = sourceURL;
          String field = null;
              Object value = null;
            Identification id = Identification.idForString(requestedPath);
View Full Code Here


        output.append("{data:");
        output.append(JsponEncoding.makeList(clientObjectsChangeIdNeeded.entrySet(), new JsponEncoding.ItemHandler() {
          public String handleItem(Object object) {
            Map.Entry<String,Persistable> clientObject = (Map.Entry<String,Persistable>) object;
            Persistable obj = clientObject.getValue();
            shouldSerialize(obj);
            return "{aliasId:\"" + clientObject.getKey() + "\",id:\"" + obj.getId() +"\"}";
          }
        }));
        output.append("}");
        clientObjectsChangeIdNeeded.clear();
        return output.toString();
View Full Code Here

      return writer.toString();
    }
    public void writeWaitingRPCs() {
      RPCMessage message = dispatchNextMessages();
      Object id = message.getId();
      Persistable result = Persevere.newObject();
      result.put("id", result, id);
      if (message instanceof RPCall) {
        result.put("object",result,((RPCall) message).getTarget());
        result.put("method",result,((RPCall) message).getFunctionName());
        result.put("params",result,new PersistableArray(((RPCall) message).getParameters()));
      }
      else if (message instanceof RPCResponse) {
        result.put("result",result,((RPCResponse) message).getResult());
        result.put("error",result,((RPCResponse) message).getError());
      }
      else
        throw new RuntimeException("Unknown RPC message: " + message);
     
      DataSerializer.serialize(result, getCurrentObjectResponse().getHttpRequest().getHeader("Accept"));
View Full Code Here

          switch (state) {
          case HISTORY:
            throw new IllegalStateException("Transaction instance can't be an array");
          case FROZEN_VERSION:
          case CHANGED:
            Persistable object = value instanceof Persistable ? (Persistable) value : objId.getTarget();
            writeObject(object, raf, state, null, null);
            break;
          case REFERENCE_IF_EXISTS:
            if (objId.isPersisted()) {
              raf.writeByte(HEADER_REFERENCE_ARRAY);
              InternalId internalId = convertIdToInternalObject(objId);
              raf.writeInt(internalId.tableId);
              writeEntity(internalId.subObjectId, raf, null);
            } else {
              throw new IllegalStateException();
            }

          }

        } else {
          switch (state) {
          case FROZEN_VERSION:
          case CHANGED:
          case HISTORY:
            Persistable object = value instanceof Persistable ? (Persistable) value : objId.getTarget();
            writeObject(object, raf, state, null, null);
            break;
          case REFERENCE_IF_EXISTS:
            if (objId.isPersisted()) {
              raf.writeByte(HEADER_REFERENCE);
View Full Code Here

  }

  Map<Long, Persistable> referenceToVersions = new WeakValueHashMap<Long, Persistable>(100);

  Persistable getVersionByReference(long reference) throws IOException {
    Persistable version;
    synchronized (referenceToVersions) {
      version = referenceToVersions.get(reference);
    }
    if (version == null) {
      getTransactionsReadFile().seek(reference);
View Full Code Here

              changes.add(changedObject);
            }
          }
        }
        historyInitializer.setProperty("changes", changes);
        Persistable newHistory = historyInitializer.getInitializingObject();
        long transactionWriteLockTime = 0;
        if(Method.profiling)
          transactionWriteLockTime = Method.startTiming();
        synchronized (transactionLogFile) { // we must write one at a time
          thisCommit = nextCommit++;
View Full Code Here

    public Persistable getPreviousVersion() {
      if (previousVersionReference == -1)
        return null;
      try {
        Persistable previous = getVersionByReference(previousVersionReference);
        ObjectVersion previousVersion = (ObjectVersion) previous.getVersion();
        if (previousVersion.isCurrent) {
          previousVersion.isCurrent = false;
          RandomAccessFile transactionsFile = getTransactionsReadFile();
          transactionsFile.seek(previousVersionReference + 1);
          transactionsFile.writeByte(0); // set it to be a non-current version
View Full Code Here

        try {
          while (processingTableUpdate > lastUpdatedFromTable) {
            //TODO: this iterates in reverse, so need to find someway to do partial indexes
            //if(numIndexed++ > MAX_TO_INDEX_AT_ONCE && !"id".equals(propertyName))
            //throw new QueryCantBeHandled("Too many items to index at once");
            Persistable lastChangeObject = getVersionByReference(processingTableUpdate);
            Persistable previousObject = lastChangeObject;
            ObjectVersion versionInfo = (ObjectVersion) lastChangeObject.getVersion();
            boolean isDeleted = versionInfo.isDeleted;
            // versionInfo.previousTableChange != processingTableUpdate: "previous  version points to self";
            if (versionInfo.currentVersionReference != processingTableUpdate || versionInfo.previousTableChange >= processingTableUpdate) {
              getVersionByReference(processingTableUpdate);
              throw new IllegalStateException("should not be here");
            }
            long nextTableUpdate = versionInfo.previousTableChange;
            if (versionInfo.isCurrent || writeListener.containsOverWrittenObject(processingTableUpdate)) {
              //System.err.println("bringUpToDate " + lastChangeObject.getId() + " isDelete " + isDeleted + " processing " + processingTableUpdate);
              boolean searching = true;
              do {
                versionInfo = (ObjectVersion) previousObject.getVersion();
                if (versionInfo.previousVersionReference <= lastUpdatedFromTable)
                  searching = false;
                previousObject = versionInfo.getPreviousVersion();
                //System.err.println("bringUpToDate " + lastChangeObject.getId() + " isDelete " + isDeleted + " processing " + processingTableUpdate + " previous " + previousObject);
                // TODO: If we know a point at which the index has definitely not been updated, we don't need to do this removal if versionInfo.previousVersionReference > knownNotHaveBeenIndexed
                if (previousObject != null) {
                  submitIfNeeded(addToObjectsNeedingIndex(new IndexNodeUpdate("id".equals(propertyName) ? convertIdToInternalObject(previousObject
                      .getId()).subObjectId : previousObject.get(propertyName), false, versionInfo.previousVersionReference, this)));
                }
              } while (searching);
              //System.err.println("numIndexed " + (numIndexed++));
              //System.out.println("addin to index " + processingTableUpdate + " " + nextTableUpdate + " " + (this instanceof RootIndexNode ? ((RootIndexNode)this).propertyName : ""));
              if(!isDeleted){
View Full Code Here

                  }
                }
                Persevere.createNewTable(path, "Object");
                return null;
              }
              Persistable datedTarget = null;
              if ("POST".equals(method)) // This is a hack to get POSTs to work on slice operator
                path = path.replaceAll("\\[.*\\:.*\\]", "");
              target = Client.getCurrentObjectResponse().requestData(path, "PUT".equals(method));
              if (targetId instanceof ObjectPath) {
                datedTarget = ((ObjectPath) targetId).getSecondToLastTarget();
              } else if (target instanceof Persistable)
                datedTarget = (Persistable) target;
              String subscribe = getParameter(request, "Subscribe");
              if (subscribe == null)
                subscribe = getParameterFromQueryString(request, "subscribe");
              // handle subscription requests
              if (null != subscribe) {
                final Identification<? extends Object> id = Identification.idForString(path);
                if (datedTarget != null) {
                  if (target instanceof ObservablePersistable)
                    ((ObservablePersistable) target).subscribe();

                  // TODO: Only do this if the range references "now"
                  request.setAttribute("org.persvr.servletNow", new Date());
                  Map<String, String> headers = new HashMap<String, String>();
                  Enumeration headerEnum = request.getHeaderNames();
                  while (headerEnum.hasMoreElements()) {
                    String headerName = (String) headerEnum.nextElement();
                    String value = request.getHeader(headerName);
                    headerName = headerName.toLowerCase();
                    if ("opera-range".equals(headerName))
                      headerName = "range";
                    headers.put(headerName, value);
                  }
                  headers.put("__now__", new Date().getTime() + "");
                  headers.put("__pathInfo__", path);
                  if (connectionId == null) {

                  } else {
                    if ("none".equals(subscribe)) {// far future, unsubscribe
                      requestHelper.connection.removeSubscription(headers);
                    } else if ("*".equals(subscribe)) {
                      requestHelper.connection.addSubscription(headers);
                      PersistableObject.addListener(requestHelper.connection);
                    }
                  }
                  setHeader("Subscribed", "OK", headerTarget);

                }
              }
              byte[] postBytes = null;
              String embeddedContent = null;
              if ("application/x-www-form-urlencoded".equals(request.getContentType())) {
                // if it was POSTed from cross-domain, it may specify it's content in the http-content parameter in the entity
                if (postBytes == null)
                  postBytes = IOUtils.toByteArray(request.getInputStream());
                String postBody = new String(postBytes, "UTF-8");
                embeddedContent = getParameterFromUrlEncoded(postBody, "http-content");
                if (embeddedContent == null)
                  embeddedContent = getParameterFromQueryString(request, "http_content");
                if (embeddedContent != null)
                  request.setAttribute("cross-site", true);
              } else if (!"GET".equals(method)) {
                // if it was a GET (that specifies a method beside GET with http-method) from cross-domain,
                //  it may specify it's content in the http-content parameter in the URL
                embeddedContent = getParameterFromQueryString(request, "http-content");
                if (embeddedContent == null)
                  embeddedContent = getParameterFromQueryString(request, "http_content");
                if (embeddedContent != null)
                  request.setAttribute("cross-site", true);
              }
              if (embeddedContent != null) {
                postBytes = embeddedContent.getBytes("UTF-8");
              }
              // Restart here
              String precondition = getParameter(request, "If");
              //TODO: Handle relative expressions
              if (precondition != null && !ScriptRuntime.toBoolean(Identification.idForString(precondition).getTarget()))
                throw new ConditionFailedException("Condition " + precondition + " was not satisfied");
              contentType = contentType == null ? null : contentType.split(";")[0];
              if ("POST".equals(method)) {
                Object postObject = null;
                boolean isJson = false;
                boolean safeContentType = false;
                if (couldBeJson(contentType)) {
                  try {
                    // this means that the content may be JSON, we are forgiving if URL encoding is used as it is
                    // the default for Ajax requests
                    if (postBytes == null)
                      postBytes = IOUtils.toByteArray(request.getInputStream());
                    String postBody = new String(postBytes, "UTF-8");
                    postObject = requestHelper.parseJsponString(postBody); // TODO: Shouldn't do this twice
                    // If it parsed we can be assured that it wasn't cross-site browser generated
                    requestHelper.authorizeCookieAuthentication();
                    // detect JSON-RPC calls
                    if (postObject instanceof Map && ((Map) postObject).containsKey("id")
                        && ((Map) postObject).containsKey("method") && ((Map) postObject).containsKey("params")
                        && ((Map) postObject).get("params") instanceof List) {
                      // It looks like a JSON-RPC request, treat it as a method call
                      requestHelper.handleRPC(target, (Map) postObject);
                      // we set the username again because it may have changed during the RPC
                      username = UserSecurity.getUserName(UserSecurity.currentUser());
                      // we set the username so the client side can access it
                      setHeader("Username", "public".equals(username) ? null : username, headerTarget);
                      // write out the response (this could actually be a request from the server)
                      Client.getCurrentObjectResponse().writeWaitingRPCs();
                      return null;
                    }
                    Object bodyData = requestHelper.convertParsedToObject(postObject);
                    String newLocation = request.getHeader("Content-ID");
                    // this indicates that the request has a client-assigned id to use temporarily
                    if (newLocation != null) {
                      // get rid of the brackets and the contextPath
                      newLocation = newLocation.substring(request.getContextPath().length() + 2, newLocation.length() - 1);
                      Client client = Client.getCurrentObjectResponse().getConnection();
                      Persistable createdObject = client.getClientSideObject(newLocation);
                      if (createdObject == null)
                        client.clientSideObject(newLocation, createdObject = Persevere.newObject(((Persistable) target)
                            .getId()));
                      target = createdObject;
                      for (Map.Entry<String, Object> entry : ((Persistable) bodyData).entrySet(0)) {
                        createdObject.set(entry.getKey(), entry.getValue());
                      }
                    } else {
                      target = postObject((Persistable) target, bodyData);
                    }
                    newContent = true;
                    isJson = true;
                  } catch (JSONParser.JSONException e) {
                    // if the content type explicity said it was JSON or JavaScript we will throw an error
                    if (contentType == null || contentType.indexOf("json") > 0 || contentType.indexOf("javascript") > 0) {
                      throw e;
                    }
                  }

                } else
                  safeContentType = true;
                if (!isJson) {
                  // it wasn't JSON (couldn't be parsed) and so we treat it as a file
                  if (ServletFileUpload.isMultipartContent(request)) {
                    if(target instanceof List){
                      target = Persevere.newObject(((Persistable) target).getId());
                    }
                    // Create a factory for disk-based file items
                    FileItemFactory factory = new DiskFileItemFactory();

                    // Create a new file upload handler
                    ServletFileUpload upload = new ServletFileUpload(factory);

                    // Parse the request
                    List<FileItem> items = upload.parseRequest(request);
                    for (FileItem item : items) {
                      if (item.isFormField()) {
                        ((Persistable) target).set(item.getFieldName(), item.getString());
                      } else {
                        Persistable fileTarget = createFile(item.getContentType(), null, item.getInputStream());
                        fileTarget.set("name", item.getName());
                        if (!(target instanceof List)) {
                          ((Persistable) target).set(item.getFieldName(), fileTarget);
                        }
                      }
                    }
                  } else {
                    if (safeContentType)
                      requestHelper.authorizeCookieAuthentication();
                    DataSource source = ((Persistable) target).getId().source;
                    String contentDisposition = request.getHeader("Content-Disposition");
                    if (postBytes == null)
                      postBytes = IOUtils.toByteArray(request.getInputStream());
                    // create a File using the provided file/binary data
                    Persistable fileTarget = createFile(contentType, contentDisposition, postBytes);
                    if (!(source instanceof DynaFileDBSource)) {
                      Persistable resourceTarget = Persevere.newObject(((Persistable) target).getId());
                      resourceTarget.put("representation:" + contentType, resourceTarget, fileTarget);
                      ((PersistableObject) resourceTarget).setAttributes("representation:" + contentType,
                          ScriptableObject.DONTENUM);
                    }
                    target = fileTarget;
                    newContent = true;
                    force204 = true;
                  }
                }

                response.setStatus(201);
              } else if ("PUT".equals(method)) {
                boolean isJson = false;
                Identification<? extends Object> id = Identification.idForString(path);
                try {
                  if (couldBeJson(contentType)) {
                    if (postBytes == null)
                      postBytes = IOUtils.toByteArray(request.getInputStream());

                    String postBody = new String(postBytes, "UTF-8");
                    if (id instanceof ObjectPath) {
                      Object value = id.getTarget();
                      if (value instanceof Persistable) {
                        id = ((Persistable) value).getId();
                      }
                    }
                    target = requestHelper.convertWithKnownId(postBody, id);

                    isJson = true;
                  }
                } catch (JSONParser.JSONException e) {
                  if (contentType == null || contentType.indexOf("json") > 0 || contentType.indexOf("javascript") > 0) {
                    throw e;
                  }
                }
                if (!isJson) {
                  // create an alternate representation for the target object using the provided file/binary data
                  DataSource source = target instanceof Persistable ? ((Persistable) target).getId().source : null;
                  String contentDisposition = request.getHeader("Content-Disposition");
                  if (postBytes == null)
                    postBytes = IOUtils.toByteArray(request.getInputStream());
                  Persistable fileTarget = createFile(contentType, contentDisposition, postBytes);
                  if (!(source instanceof DynaFileDBSource) && !(id instanceof ObjectPath)) {
                    if (target instanceof Persistable) {
                      ((Persistable) target).set("representation:" + contentType, fileTarget);
                      ((PersistableObject) target).setAttributes("representation:" + contentType, ScriptableObject.DONTENUM);
                    }
                  }
                  newContent = true;
                  force204 = true;
                  target = fileTarget;
                }
                if (id instanceof ObjectPath) {
                  // if it is a path we set the value of the property
                  Object key = ((ObjectPath) id).getLastPath();
                  Persistable secondToLastTarget = (Persistable) ((ObjectPath) id).getSecondToLastTarget();
                  if (key instanceof Integer)
                    secondToLastTarget.put((Integer) key, secondToLastTarget, target);
                  else {
                    secondToLastTarget.set((String) key, target);
                  }
                } else if (!(target instanceof Persistable))
                  //TODO: This should be allowed if an ObjectPath is used
                  throw new RuntimeException("Can not replace an identified object with a primitive value");
                if (!isJson) {
                  target = Undefined.instance;// no need to return the file just uploaded
                }

              } else if ("DELETE".equals(method)) {
                // delete the target object or property
                Identification<? extends Object> id = Identification.idForString(path);
                if (id instanceof ObjectPath) {
                  // it is a property
                  Object key = ((ObjectPath) id).getLastPath();
                  Persistable secondToLastTarget = (Persistable) ((ObjectPath) id).getSecondToLastTarget();
                  if (key instanceof Integer)
                    secondToLastTarget.delete((Integer) key);
                  else
                    secondToLastTarget.delete((String) key);
                  return null;
                } else if (id instanceof JsonPath) {
                  //TODO: Needs to be a way to do delete items from JSONPath queries
                  for (int i = ((List) target).size(); i > 0;) {
                    i--;
                    Persistable item = ((Persistable) ((List) target).get(i));
                    item.delete();
                  }
                  return null;
                }
                if (target instanceof Persistable) {
                  ((Persistable) target).delete();
View Full Code Here

      //export.excludedObjects.add((DataObject)DataSourceManager.getRootObject().getById(new Id("226")));
      export.excludedObjects.add((Persistable)DataSourceManager.getById("dyna/1709"));
      export.excludedObjects.add((Persistable)DataSourceManager.getById("dyna/3"));
      export.topLevelObjects.add((Persistable)DataSourceManager.getRootObject());
      while(!export.topLevelObjects.isEmpty()) {
        Persistable objectToExport = export.topLevelObjects.iterator().next();
        export.export(objectToExport,false);
        export.topLevelObjects.remove(objectToExport);
      }
      export.write("{}]}");
      export.out.close();
View Full Code Here

TOP

Related Classes of org.persvr.data.Persistable

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.