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 will do output. */
047        public boolean getDoOutput();
048    
049        /** @return true if the operation will be redirected. */
050        public boolean getRedirect();
051    
052        /** @return true the expected http response code. */
053        public int getExpectedHttpResponseCode();
054    
055        /** @return a URI query string. */
056        public String toQueryString();
057      }
058    
059      /** Expects HTTP response 307 "Temporary Redirect". */
060      public static class TemporaryRedirectOp implements Op {
061        static final TemporaryRedirectOp CREATE = new TemporaryRedirectOp(
062            PutOpParam.Op.CREATE);
063        static final TemporaryRedirectOp APPEND = new TemporaryRedirectOp(
064            PostOpParam.Op.APPEND);
065        static final TemporaryRedirectOp OPEN = new TemporaryRedirectOp(
066            GetOpParam.Op.OPEN);
067        static final TemporaryRedirectOp GETFILECHECKSUM = new TemporaryRedirectOp(
068            GetOpParam.Op.GETFILECHECKSUM);
069        
070        static final List<TemporaryRedirectOp> values
071            = Collections.unmodifiableList(Arrays.asList(
072                new TemporaryRedirectOp[]{CREATE, APPEND, OPEN, GETFILECHECKSUM}));
073    
074        /** Get an object for the given op. */
075        public static TemporaryRedirectOp valueOf(final Op op) {
076          for(TemporaryRedirectOp t : values) {
077            if (op == t.op) {
078              return t;
079            }
080          }
081          throw new IllegalArgumentException(op + " not found.");
082        }
083    
084        private final Op op;
085    
086        private TemporaryRedirectOp(final Op op) {
087          this.op = op;
088        }
089    
090        @Override
091        public Type getType() {
092          return op.getType();
093        }
094    
095        @Override
096        public boolean getDoOutput() {
097          return op.getDoOutput();
098        }
099    
100        @Override
101        public boolean getRedirect() {
102          return false;
103        }
104    
105        /** Override the original expected response with "Temporary Redirect". */
106        @Override
107        public int getExpectedHttpResponseCode() {
108          return Response.Status.TEMPORARY_REDIRECT.getStatusCode();
109        }
110    
111        @Override
112        public String toQueryString() {
113          return op.toQueryString();
114        }
115      }
116    
117      /** @return the parameter value as a string */
118      @Override
119      public String getValueString() {
120        return value.toString();
121      }
122    
123      HttpOpParam(final Domain<E> domain, final E value) {
124        super(domain, value);
125      }
126    }