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/index requests. 
014     * Requires the admin user.
015     * 
016     * @author Philip Johnson
017     */
018    public class IndexResource 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 IndexResource(Context context, Request request, Response response) {
028        super(context, request, response);
029      }
030    
031      /**
032       * Returns 200 if index command succeeded. This requires admin authorization.
033       * 
034       * @param variant Ignored.
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.indexTables();
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       * Indicate the PUT method is supported.
054       * 
055       * @return True.
056       */
057      @Override
058      public boolean allowPut() {
059        return true;
060      }
061    
062      /**
063       * Indicate that GET is not supported.
064       * @return False.
065       */
066      @Override
067      public boolean allowGet() {
068        return false;
069      }
070    
071      /**
072       * Get is not supported, but the method must be implemented.
073       * 
074       * @param variant Ignored.
075       * @return Null.
076       */
077      @Override
078      public Representation represent(Variant variant) {
079        getResponse().setStatus(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED);
080        return null;
081      }
082    
083    }