001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.hadoop.hdfs.web.resources;
019    
020    import javax.ws.rs.core.Response;
021    
022    
023    /** Http operation parameter. */
024    public abstract class HttpOpParam<E extends Enum<E> & HttpOpParam.Op>
025        extends EnumParam<E> {
026      /** Parameter name. */
027      public static final String NAME = "op";
028    
029      /** Default parameter value. */
030      public static final String DEFAULT = NULL;
031    
032      /** Http operation types */
033      public static enum Type {
034        GET, PUT, POST, DELETE;
035      }
036    
037      /** Http operation interface. */
038      public static interface Op {
039        /** @return the Http operation type. */
040        public Type getType();
041    
042        /** @return true if the operation will do output. */
043        public boolean getDoOutput();
044    
045        /** @return true the expected http response code. */
046        public int getExpectedHttpResponseCode();
047    
048        /** @return a URI query string. */
049        public String toQueryString();
050      }
051    
052      /** Expects HTTP response 307 "Temporary Redirect". */
053      public static class TemporaryRedirectOp implements Op {
054        static final TemporaryRedirectOp CREATE = new TemporaryRedirectOp(PutOpParam.Op.CREATE);
055        static final TemporaryRedirectOp APPEND = new TemporaryRedirectOp(PostOpParam.Op.APPEND);
056        
057        /** Get an object for the given op. */
058        public static TemporaryRedirectOp valueOf(final Op op) {
059          if (op == CREATE.op) {
060            return CREATE;
061          } else if (op == APPEND.op) {
062            return APPEND;
063          }
064          throw new IllegalArgumentException(op + " not found.");
065        }
066    
067        private final Op op;
068    
069        private TemporaryRedirectOp(final Op op) {
070          this.op = op;
071        }
072    
073        @Override
074        public Type getType() {
075          return op.getType();
076        }
077    
078        @Override
079        public boolean getDoOutput() {
080          return op.getDoOutput();
081        }
082    
083        /** Override the original expected response with "Temporary Redirect". */
084        @Override
085        public int getExpectedHttpResponseCode() {
086          return Response.Status.TEMPORARY_REDIRECT.getStatusCode();
087        }
088    
089        @Override
090        public String toQueryString() {
091          return op.toQueryString();
092        }
093      }
094    
095      /** @return the parameter value as a string */
096      @Override
097      public String getValueString() {
098        return value.toString();
099      }
100    
101      HttpOpParam(final Domain<E> domain, final E value) {
102        super(domain, value);
103      }
104    }