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 java.util.Arrays;
021    import java.util.Collections;
022    import java.util.List;
023    
024    import javax.ws.rs.core.Response;
025    
026    
027    /** Http operation parameter. */
028    public abstract class HttpOpParam<E extends Enum<E> & HttpOpParam.Op>
029        extends EnumParam<E> {
030      /** Parameter name. */
031      public static final String NAME = "op";
032    
033      /** Default parameter value. */
034      public static final String DEFAULT = NULL;
035    
036      /** Http operation types */
037      public static enum Type {
038        GET, PUT, POST, DELETE;
039      }
040    
041      /** Http operation interface. */
042      public static interface Op {
043        /** @return the Http operation type. */
044        public Type getType();
045    
046        /** @return true if the operation cannot use a token */
047        public boolean getRequireAuth();
048        
049        /** @return true if the operation will do output. */
050        public boolean getDoOutput();
051    
052        /** @return true if the operation will be redirected. */
053        public boolean getRedirect();
054    
055        /** @return true the expected http response code. */
056        public int getExpectedHttpResponseCode();
057    
058        /** @return a URI query string. */
059        public String toQueryString();
060      }
061    
062      /** Expects HTTP response 307 "Temporary Redirect". */
063      public static class TemporaryRedirectOp implements Op {
064        static final TemporaryRedirectOp CREATE = new TemporaryRedirectOp(
065            PutOpParam.Op.CREATE);
066        static final TemporaryRedirectOp APPEND = new TemporaryRedirectOp(
067            PostOpParam.Op.APPEND);
068        static final TemporaryRedirectOp OPEN = new TemporaryRedirectOp(
069            GetOpParam.Op.OPEN);
070        static final TemporaryRedirectOp GETFILECHECKSUM = new TemporaryRedirectOp(
071            GetOpParam.Op.GETFILECHECKSUM);
072        
073        static final List<TemporaryRedirectOp> values
074            = Collections.unmodifiableList(Arrays.asList(
075                new TemporaryRedirectOp[]{CREATE, APPEND, OPEN, GETFILECHECKSUM}));
076    
077        /** Get an object for the given op. */
078        public static TemporaryRedirectOp valueOf(final Op op) {
079          for(TemporaryRedirectOp t : values) {
080            if (op == t.op) {
081              return t;
082            }
083          }
084          throw new IllegalArgumentException(op + " not found.");
085        }
086    
087        private final Op op;
088    
089        private TemporaryRedirectOp(final Op op) {
090          this.op = op;
091        }
092    
093        @Override
094        public Type getType() {
095          return op.getType();
096        }
097    
098        @Override
099        public boolean getRequireAuth() {
100          return op.getRequireAuth();
101        }
102    
103        @Override
104        public boolean getDoOutput() {
105          return op.getDoOutput();
106        }
107    
108        @Override
109        public boolean getRedirect() {
110          return false;
111        }
112    
113        /** Override the original expected response with "Temporary Redirect". */
114        @Override
115        public int getExpectedHttpResponseCode() {
116          return Response.Status.TEMPORARY_REDIRECT.getStatusCode();
117        }
118    
119        @Override
120        public String toQueryString() {
121          return op.toQueryString();
122        }
123      }
124    
125      /** @return the parameter value as a string */
126      @Override
127      public String getValueString() {
128        return value.toString();
129      }
130    
131      HttpOpParam(final Domain<E> domain, final E value) {
132        super(domain, value);
133      }
134    }