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 */
018package org.apache.hadoop.hdfs.web.resources;
019
020import javax.ws.rs.core.Response;
021
022
023/** Http operation parameter. */
024public 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 cannot use a token */
043    public boolean getRequireAuth();
044    
045    /** @return true if the operation will do output. */
046    public boolean getDoOutput();
047
048    /** @return true the expected http response code. */
049    public int getExpectedHttpResponseCode();
050
051    /** @return a URI query string. */
052    public String toQueryString();
053  }
054
055  /** Expects HTTP response 307 "Temporary Redirect". */
056  public static class TemporaryRedirectOp implements Op {
057    static final TemporaryRedirectOp CREATE = new TemporaryRedirectOp(PutOpParam.Op.CREATE);
058    static final TemporaryRedirectOp APPEND = new TemporaryRedirectOp(PostOpParam.Op.APPEND);
059    
060    /** Get an object for the given op. */
061    public static TemporaryRedirectOp valueOf(final Op op) {
062      if (op == CREATE.op) {
063        return CREATE;
064      } else if (op == APPEND.op) {
065        return APPEND;
066      }
067      throw new IllegalArgumentException(op + " not found.");
068    }
069
070    private final Op op;
071
072    private TemporaryRedirectOp(final Op op) {
073      this.op = op;
074    }
075
076    @Override
077    public Type getType() {
078      return op.getType();
079    }
080
081    @Override
082    public boolean getRequireAuth() {
083      return op.getRequireAuth();
084    }
085
086    @Override
087    public boolean getDoOutput() {
088      return op.getDoOutput();
089    }
090
091    /** Override the original expected response with "Temporary Redirect". */
092    @Override
093    public int getExpectedHttpResponseCode() {
094      return Response.Status.TEMPORARY_REDIRECT.getStatusCode();
095    }
096
097    @Override
098    public String toQueryString() {
099      return op.toQueryString();
100    }
101  }
102
103  /** @return the parameter value as a string */
104  @Override
105  public String getValueString() {
106    return value.toString();
107  }
108
109  HttpOpParam(final Domain<E> domain, final E value) {
110    super(domain, value);
111  }
112}