001    package org.hackystat.sensorbase.resource.db;
002    
003    import org.hackystat.sensorbase.db.DbManager;
004    import org.hackystat.sensorbase.resource.sensorbase.SensorBaseResource;
005    import org.restlet.Context;
006    import org.restlet.data.Request;
007    import org.restlet.data.Response;
008    import org.restlet.data.Status;
009    import org.restlet.resource.Representation;
010    import org.restlet.resource.Variant;
011    
012    /**
013     * Implements the Resource for processing PUT {host}/db/table/compress requests. 
014     * Requires the admin user.
015     * 
016     * @author Philip Johnson
017     */
018    public class CompressResource extends SensorBaseResource {
019    
020      /**
021       * The standard constructor.
022       * 
023       * @param context The context.
024       * @param request The request object.
025       * @param response The response object.
026       */
027      public CompressResource(Context context, Request request, Response response) {
028        super(context, request, response);
029      }
030    
031      /**
032       * Returns 200 if compress command succeeded. This requires admin authorization.
033       * 
034       * @param variant The representational variant requested.
035       */
036      @Override
037      public void storeRepresentation(Representation variant) {
038        try {
039          if (!validateAuthUserIsAdmin()) {
040            return;
041          }
042          DbManager dbManager = (DbManager) this.server.getContext().getAttributes().get("DbManager");
043          boolean success = dbManager.compressTables();
044          Status status = (success) ? Status.SUCCESS_OK : Status.SERVER_ERROR_INTERNAL;
045          getResponse().setStatus(status);
046        }
047        catch (RuntimeException e) {
048          setStatusInternalError(e);
049        }
050    
051      }
052        
053    
054      /**
055       * Indicate the PUT method is supported.
056       * 
057       * @return True.
058       */
059      @Override
060      public boolean allowPut() {
061        return true;
062      }
063    
064      /**
065       * Indicate that GET is not supported.
066       * @return False.
067       */
068      @Override
069      public boolean allowGet() {
070        return false;
071      }
072    
073      /**
074       * Get is not supported, but the method must be implemented.
075       * 
076       * @param variant Ignored.
077       * @return Null.
078       */
079      @Override
080      public Representation represent(Variant variant) {
081        getResponse().setStatus(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED);
082        return null;
083      }
084    
085    }