Package org.restlet.data

Examples of org.restlet.data.Status


      // Store in XML DB
      AtomResource.mergeEntry(top,entry.getUUID(),entry.getCreated(),entry.getEdited(),authorName,null);

      try {
         Status status = storage.storeEntry(feed.getPath(),feed.getUUID(),entry.getUUID(),entryDoc);
         if (!status.isSuccess()) {
            String xml = null;
            try {
               StringWriter w = new StringWriter();
               XMLWriter.writeDocument(entryDoc,w);
               xml = w.toString();
               //log.info(xml);
            } catch (Exception ex) {
               // TODO: need to delete
               log.log(Level.SEVERE,"Cannot serialize entry for error message.",ex);
            }
            throw new AppException(Status.SERVER_ERROR_INTERNAL,"Cannot store entry, status="+status.getCode()+"\n"+xml);
         }
        
         storage.feedUpdated(feed.getPath(),feed.getUUID(),feed.getEdited());
        
      } catch (SQLException ex) {
View Full Code Here


            throw new AppException(Status.SERVER_ERROR_INTERNAL,"Cannot decode slug for media entry title.",ex);
         }

         try {
            String path = feed.getPath();
            Status entryStatus = storage.storeEntry(path,feed.getUUID(),entry.getUUID(),doc);
            if (entryStatus.isSuccess()) {
               Status mediaStatus = storage.storeMedia(path,feed.getUUID(),media.getName(),mediaType,is);
               // TODO: storage may change media type parameters and the entry needs to be updated
               if (!mediaStatus.isSuccess()) {
                  try {
                     entry.delete(null);
                  } catch (SQLException ox) {
                     log.log(Level.SEVERE,"Cannot delete entry for exception cleanup.",ox);
                  }
View Full Code Here

      // merge the data with the new entry
      AtomResource.mergeEntry(top,entry.getUUID(),entry.getCreated(),modified,authorName,media);

      try {
         Status status = storage.storeEntry(feed.getPath(),feed.getUUID(),entry.getUUID(),doc);
         if (!status.isSuccess()) {
            throw new AppException(Status.SERVER_ERROR_INTERNAL,"Cannot store entry document.");
         }
         storage.feedUpdated(feed.getPath(),feed.getUUID(),feed.getEdited());
      } catch (IOException ex) {
         throw new AppException(Status.SERVER_ERROR_INTERNAL,"Cannot store entry document.",ex);
View Full Code Here

               throw new IOException(ex.getMessage());
            }
         }
      });
      Response response = client.handle(request);
      Status status = response.getStatus();
      boolean hasEntity = false;
      try {
         hasEntity = response.isEntityAvailable();
         if (status.isSuccess() && hasEntity) {
            XMLRepresentationParser parser = new XMLRepresentationParser();
            Document doc = parser.load(response.getEntity());
            this.entry = new Entry(doc);
         }
      } finally {
View Full Code Here

               // Make sure to remove the xml:base on the feed element for storage;
               feedDoc.getDocumentElement().getAttributes().remove(Attribute.XML_BASE);

               if (!targetClient.exists()) {
                  log.info("Creating target feed "+targetClient.getLocation());
                  Status status = targetClient.create(feedDoc);
                  if (!status.isSuccess()) {
                     log.severe("Cannot create target feed, status="+status.getCode());
                     errorCount++;
                     ok = false;
                  }
               }
            }
            public void onEntry(Document entryDoc) {
               if (!ok) {
                  return;
               }
               Entry entry = new Entry(entryDoc);
               entry.index();

               UUID entryId = null;
               try {
                  entryId = UUID.fromString(entry.getId().substring(9));
               } catch (IllegalArgumentException ex) {
                  log.severe("Ignoring entry with bad UUID: "+entry.getId());
                  errorCount++;
                  return;
               }
               log.info("Entry: "+entryId);
               EntryClient entryClient = new EntryClient(new Reference(targetClient.getEntryLocation(entryId).toString()),target.getIdentity());
               String src = null;
               Element content = entryDoc.getDocumentElement().getFirstElementNamed(CONTENT_NAME);
               URI baseURI = null;
               MediaType contentType = null;
               if (content!=null) {
                  src = content.getAttributeValue("src");
                  String type = content.getAttributeValue("type");
                  if (type!=null) {
                     contentType = MediaType.valueOf(type);
                  }
                  baseURI = content.getBaseURI();
               }
               if (entries!=null) {
                  entries.add(entryId);
               }
              
               Status exists = null;
               try {
                  exists = entryClient.get();
               } catch (Exception ex) {
                  log.log(Level.SEVERE,"Cannot get entry "+entryClient.getLocation()+", error: "+ex.getMessage(),ex);
                  errorCount++;
                  return;
               }
               if (exists.isSuccess()) {
                  entryClient.getEntry().index();
                  try {
                    
                     Status status = entryClient.update(entryDoc);
                     if (!status.isSuccess()) {
                        log.severe("Cannot update entry "+entryClient.getLocation()+", status="+status.getCode());
                        errorCount++;
                     }
                  } catch (Exception ex) {
                     log.log(Level.SEVERE,"Cannot update entry "+entryClient.getLocation()+", error: "+ex.getMessage(),ex);
                     errorCount++;
                  }
                  if (src!=null) {
                     // update media
                     Reference mediaLocation = new Reference(baseURI.resolve(src).toString());
                     Client client = new Client(mediaLocation.getSchemeProtocol());
                     Request getRequest = new Request(Method.GET,mediaLocation);
                     if (source.getIdentity()!=null) {
                        getRequest.setChallengeResponse(new ChallengeResponse(ChallengeScheme.HTTP_BASIC,source.getUsername(),source.getPassword()));
                     }
                     Response getResponse = client.handle(getRequest);
                     if (getResponse.getStatus().isSuccess()) {
                        Reference targetLocation = new Reference(targetClient.getLocation().resolve(src).toString());
                        Client targetClient = new Client(targetLocation.getSchemeProtocol());
                        Request putRequest = new Request(Method.PUT,targetLocation);
                        if (target.getIdentity()!=null) {
                           putRequest.setChallengeResponse(new ChallengeResponse(ChallengeScheme.HTTP_BASIC,target.getUsername(),target.getPassword()));
                        }
                        Representation rep = getResponse.getEntity();
                        if (contentType!=null) {
                           rep.setMediaType(contentType);
                        }
                        putRequest.setEntity(rep);
                        Response putResponse = targetClient.handle(putRequest);
                        if (!putResponse.getStatus().isSuccess()) {
                           log.severe("Cannot put media update to "+targetLocation+", status="+putResponse.getStatus().getCode());
                        }
                     } else {
                        if (!getResponse.getStatus().isSuccess()) {
                           log.severe("Cannot get media from "+mediaLocation+", status="+getResponse.getStatus().getCode());
                        }
                     }
                  }
               } else if (exists.getCode()==Status.CLIENT_ERROR_NOT_FOUND.getCode()) {
                  if (src==null) {
                     // regular entry
                     try {
                        targetClient.createEntry(entryDoc);
                     } catch (StatusException ex) {
                        log.severe("Cannot create entry "+entryClient.getLocation()+", status="+ex.getStatus().getCode());
                        errorCount++;
                     } catch (Exception ex) {
                        log.log(Level.SEVERE,"Cannot create entry "+entryClient.getLocation()+", error: "+ex.getMessage(),ex);
                        errorCount++;
                     }
                  } else {
                     // media entry
                     Reference mediaLocation = new Reference(baseURI.resolve(src).toString());
                     Client client = new Client(mediaLocation.getSchemeProtocol());
                     Request request = new Request(Method.GET,mediaLocation);
                     if (source.getIdentity()!=null) {
                        request.setChallengeResponse(new ChallengeResponse(ChallengeScheme.HTTP_BASIC,source.getUsername(),source.getPassword()));
                     }
                     Response response = client.handle(request);
                     if (response.getStatus().isSuccess()) {
                        try {
                           Representation rep = response.getEntity();
                           if (contentType!=null) {
                              rep.setMediaType(contentType);
                           }
                           Entry mediaEntry = targetClient.createMedia(entryId, src, rep);
                           entryClient.setEntry(mediaEntry);
                           try {
                              Status status = entryClient.update(entryDoc);
                              if (!status.isSuccess()) {
                                 log.severe("Cannot update entry "+entryClient.getLocation()+", status="+status.getCode());
                                 errorCount++;
                              }
                           } catch (Exception ex) {
                              log.log(Level.SEVERE,"Cannot update entry "+entryClient.getLocation()+", error: "+ex.getMessage(),ex);
                              errorCount++;
View Full Code Here

                     log.info("Deleting feed "+feedURI);
                     FeedClient appClient = new FeedClient(feedURI);
                     if (auth!=null) {
                        appClient.setIdentity(auth.getName(),auth.getPassword());
                     }
                     Status status = appClient.delete();
                     if (!status.isSuccess() && !status.equals(Status.CLIENT_ERROR_NOT_FOUND)) {
                        errorCount++;
                        log.severe("Cannot delete feed "+feedURI+", stopping synchronization.");
                        throw new SyncException("Synchronization was incomplete.  Stopped due to failure to delete feed: "+feedURI);
                     }
                  } else {
                     // delete entry
                     log.info("Deleting entry "+entry+" in feed "+feedURI);
                     FeedClient appClient = new FeedClient(feedURI);
                     if (auth!=null) {
                        appClient.setIdentity(auth.getName(),auth.getPassword());
                     }
                     Status status = appClient.deleteEntry(entry);
                     if (!status.isSuccess() && !status.equals(Status.CLIENT_ERROR_NOT_FOUND)) {
                        errorCount++;
                        log.severe("Cannot delete feed "+feedURI+", stopping synchronization.");
                        throw new SyncException("Synchronization was incomplete.  Stopped due to failure to delete feed: "+feedURI);
                     }
                  }
View Full Code Here

   }
  
   public Representation put(Representation entity) {
      try {
         media.setMediaType(entity.getMediaType());
         Status status = storage.storeMedia(feed.getPath(),feed.getUUID(),media.getName(),entity.getMediaType(),entity.getStream());
         if (!status.isSuccess()) {
            getResponse().setStatus(status);
            return new StringRepresentation("Cannot store media.");
         }
         media.touch();
        
         // get the media resoruce's entry
         Entry entry = media.getEntry();
        
         // Get the entry's representation
         String feedBaseURI = getRequest().getResourceRef().getParentRef().getPath();
         Representation entryRep = app.getEntryRepresentation(feedBaseURI,feed,entry.getUUID());
         // avoid a thread with the entry's output representation
         StringWriter sw = new StringWriter();
         entryRep.write(sw);
         entryRep.release();
         DocumentLoader loader = new SAXDocumentLoader();
         Document doc = loader.load(new StringReader(sw.toString()));
        
         // mark the entry as edited
         Date date = media.getEntry().edited();
        
         // change the entry's edited element
         Element edited = doc.getDocumentElement().getFirstElementNamed(AtomResource.EDITED_NAME);
         if (edited!=null) {
            edited.clear();
            edited.addCharacters(toXSDDate(date));
         }
        
         // store the change.
         status = storage.storeEntry(feed.getPath(),feed.getUUID(),entry.getUUID(),doc);
         if (status.isSuccess()) {
            getResponse().setStatus(Status.SUCCESS_NO_CONTENT);
            return null;
         } else {
            getResponse().setStatus(status);
            return new StringRepresentation("Cannot update entry for media.");
View Full Code Here

            getResponse().setStatus(Status.CLIENT_ERROR_CONFLICT);
            return new StringRepresentation("Resource already exists with name "+slug);
         }
        
         // TODO: move should use rename
         Status status = storage.storeMedia(feed.getPath(),feed.getUUID(),slug,entity.getMediaType(),entity.getStream());
         if (!status.isSuccess()) {
            getResponse().setStatus(status);
            return new StringRepresentation("Cannot store media to "+slug);
         }
         if (!storage.deleteMedia(feed.getPath(), feed.getUUID(), media.getName())) {
            getLogger().severe("Cannot delete previous entry storage "+feed.getPath()+","+feed.getUUID()+","+feed.getName());
         }
         media.setName(slug);
         media.touch();
        
        
         // get the media resoruce's entry
         Entry entry = media.getEntry();
        
         // Get the entry's representation
         String feedBaseURI = getRequest().getResourceRef().getParentRef().getPath();
         Representation entryRep = app.getEntryRepresentation(feedBaseURI,feed,entry.getUUID());
         // avoid a thread with the entry's output representation
         StringWriter sw = new StringWriter();
         entryRep.write(sw);
         entryRep.release();
         DocumentLoader loader = new SAXDocumentLoader();
         Document doc = loader.load(new StringReader(sw.toString()));
        
         // mark the entry as edited
         Date date = media.getEntry().edited();
        
         // change the entry's edited element
         Element edited = doc.getDocumentElement().getFirstElementNamed(AtomResource.EDITED_NAME);
         if (edited!=null) {
            edited.clear();
            edited.addCharacters(toXSDDate(date));
         }
         Element content = doc.getDocumentElement().getFirstElementNamed(AtomResource.CONTENT_NAME);
         content.setAttributeValue("src",slug);
        
         Iterator<Element> links = doc.getDocumentElement().getElementsByName(AtomResource.LINK_NAME);
         while (links.hasNext()) {
            Element link = links.next();
            if ("edit-media".equals(link.getAttributeValue("rel"))) {
               link.setAttributeValue("href", slug);
            }
         }
        
         // store the change.
         status = storage.storeEntry(feed.getPath(),feed.getUUID(),entry.getUUID(),doc);
         if (status.isSuccess()) {
            getResponse().setStatus(Status.SUCCESS_OK);
            Form returnHeaders = new Form();
            getResponse().getAttributes().put("org.restlet.http.headers",returnHeaders);
            Reference ref = new Reference(getRequest().getResourceRef().getParentRef()+"_/"+entry.getUUID().toString());
            getResponse().setLocationRef(ref);
View Full Code Here

    private Representation getRepresentation() {
        checkNotNull(client, request);
        answerHandler.setAcceptedMediaTypes(request);
        final Response response = client.handle(request);
        final Status status = response.getStatus();
        if (status.isSuccess()) {
            return response.getEntity();
        } else {
            return handleFail(status);
        }
    }
View Full Code Here

     *            The high-level request.
     * @return The result status.
     */
    @Override
    public Status sendRequest(Request request) {
        Status result = null;

        try {
            final Representation entity = request.getEntity();

            // Set the request headers
            for (final Parameter header : getRequestHeaders()) {
                getHttpMethod().addRequestHeader(header.getName(),
                        header.getValue());
            }

            // For those method that accept enclosing entities, provide it
            if ((entity != null)
                    && (getHttpMethod() instanceof EntityEnclosingMethod)) {
                final EntityEnclosingMethod eem = (EntityEnclosingMethod) getHttpMethod();
                eem.setRequestEntity(new RequestEntity() {
                    public long getContentLength() {
                        return entity.getSize();
                    }

                    public String getContentType() {
                        return (entity.getMediaType() != null) ? entity
                                .getMediaType().toString() : null;
                    }

                    public boolean isRepeatable() {
                        return !entity.isTransient();
                    }

                    public void writeRequest(OutputStream os)
                            throws IOException {
                        entity.write(os);
                    }
                });
            }

            // Ensure that the connection is active
            this.clientHelper.getHttpClient().executeMethod(getHttpMethod());

            // Now we can access the status code, this MUST happen after closing
            // any open request stream.
            result = new Status(getStatusCode(), null, getReasonPhrase(), null);

            // If there is no response body, immediately release the connection
            if (getHttpMethod().getResponseBodyAsStream() == null) {
                getHttpMethod().releaseConnection();
            }
        } catch (IOException ioe) {
            this.clientHelper
                    .getLogger()
                    .log(
                            Level.WARNING,
                            "An error occurred during the communication with the remote HTTP server.",
                            ioe);
            result = new Status(Status.CONNECTOR_ERROR_COMMUNICATION, ioe);

            // Release the connection
            getHttpMethod().releaseConnection();
        }

View Full Code Here

TOP

Related Classes of org.restlet.data.Status

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.