Package org.atomojo.app.admin

Source Code of org.atomojo.app.admin.SyncTargetsResource

/*
* SyncResource.java
*
* Created on April 12, 2007, 1:39 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package org.atomojo.app.admin;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Iterator;
import java.util.logging.Level;
import org.atomojo.app.App;
import org.atomojo.app.client.XMLRepresentationParser;
import org.atomojo.app.db.DB;
import org.atomojo.app.db.SyncTarget;
import org.infoset.xml.Document;
import org.infoset.xml.InfosetFactory;
import org.infoset.xml.ItemConstructor;
import org.infoset.xml.ItemDestination;
import org.infoset.xml.XMLException;
import org.infoset.xml.util.DocumentDestination;
import org.infoset.xml.util.DocumentSource;
import org.infoset.xml.util.WriterItemDestination;
import org.restlet.data.CharacterSet;
import org.restlet.data.MediaType;
import org.restlet.data.Reference;
import org.restlet.data.Status;
import org.restlet.representation.OutputRepresentation;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.ServerResource;

/**
*
* @author alex
*/
public class SyncTargetsResource extends ServerResource implements AdminXML
{
  
  
   /** Creates a new instance of SyncResource */
   public SyncTargetsResource() {
      setNegotiated(false);
   }
  
   public Representation get()
   {
      final DB db = (DB)getRequest().getAttributes().get(App.DB_ATTR);
      Representation rep = new OutputRepresentation(MediaType.APPLICATION_XML) {
         public void write(OutputStream os)
            throws IOException
         {
            try {
               Iterator<SyncTarget> targets = db.getSyncTargets();
               Writer out = new OutputStreamWriter(os,"UTF-8");
               ItemDestination dest = new WriterItemDestination(out,"UTF-8");
               ItemConstructor constructor = InfosetFactory.getDefaultInfoset().createItemConstructor();
               dest.send(constructor.createDocument());
               dest.send(constructor.createElement(AdminXML.NM_TARGETS));
               while (targets.hasNext()) {
                  SyncTarget target = targets.next();
                  target.marshall();
                  DocumentSource.generate(target.getElement(),false,dest);
               }
               dest.send(constructor.createElementEnd(AdminXML.NM_TARGETS));
               dest.send(constructor.createDocumentEnd());
               out.flush();
               out.close();
            } catch (XMLException ex) {
               getContext().getLogger().log(Level.SEVERE,"Cannot get list of targets from DB: "+ex.getMessage(),ex);
               getResponse().setStatus(Status.SERVER_ERROR_INTERNAL,"Cannot get list of targets due to XML DB error.");
            }
         }
      };
      rep.setCharacterSet(CharacterSet.UTF_8);
      return rep;
   }
  
   public Representation post(Representation entity)
   {
      if (!XMLRepresentationParser.isXML(entity.getMediaType())) {
         getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
         return new StringRepresentation("Non-XML media type for entity body: "+entity.getMediaType().getName());
      }
     
      final DB db = (DB)getRequest().getAttributes().get(App.DB_ATTR);
      XMLRepresentationParser parser = new XMLRepresentationParser();
      Document doc = null;
     
      try {
         DocumentDestination dest = new DocumentDestination();
         parser.parse(entity,AdminApplication.createAdminDocumentDestination(dest,AdminXML.NM_TARGET));
         doc = dest.getDocument();
         SyncTarget target = new SyncTarget(db,doc.getDocumentElement());
         if (target.exists()) {
            getResponse().setStatus(Status.CLIENT_ERROR_CONFLICT);
            return new StringRepresentation("Target with name "+target.getName()+" already exists.");
         }
         if (target.create()) {
            getResponse().setStatus(Status.SUCCESS_NO_CONTENT);
            Reference ref = new Reference(getRequest().getResourceRef().toString()+"/"+target.getName());
            getResponse().setLocationRef(ref);
            return null;
         } else {
            getContext().getLogger().severe("Cannot store XML for target");
            getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
            return new StringRepresentation("Internal error, see logs.");
         }
      } catch (Exception ex) {
         getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
         return new StringRepresentation("XML parse error: "+ex.getMessage());
      }
     
   }
  
}
TOP

Related Classes of org.atomojo.app.admin.SyncTargetsResource

TOP
Copyright © 2018 www.massapi.com. 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.