001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.http4;
018    
019    import org.apache.camel.Exchange;
020    import org.apache.camel.Expression;
021    import org.apache.camel.builder.ExpressionBuilder;
022    import org.apache.http.client.methods.HttpDelete;
023    import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
024    import org.apache.http.client.methods.HttpGet;
025    import org.apache.http.client.methods.HttpHead;
026    import org.apache.http.client.methods.HttpOptions;
027    import org.apache.http.client.methods.HttpPost;
028    import org.apache.http.client.methods.HttpPut;
029    import org.apache.http.client.methods.HttpRequestBase;
030    import org.apache.http.client.methods.HttpTrace;
031    
032    public enum HttpMethods implements Expression {
033    
034        GET(HttpGet.class),
035        POST(HttpPost.class),
036        PUT(HttpPut.class),
037        DELETE(HttpDelete.class),
038        HEAD(HttpHead.class),
039        OPTIONS(HttpOptions.class),
040        TRACE(HttpTrace.class);
041    
042        final Class<? extends HttpRequestBase> clazz;
043        final boolean entity;
044    
045        HttpMethods(Class<? extends HttpRequestBase> clazz) {
046            this.clazz = clazz;
047            entity = HttpEntityEnclosingRequestBase.class.isAssignableFrom(clazz);
048        }
049    
050        public HttpRequestBase createMethod(final String url) {
051            try {
052                return clazz.getDeclaredConstructor(String.class).newInstance(url);
053            } catch (Exception e) {
054                throw new RuntimeException(e);
055            }
056        }
057    
058        public final boolean isEntityEnclosing() {
059            return entity;
060        }
061    
062        public <T> T evaluate(Exchange exchange, Class<T> type) {
063            return ExpressionBuilder.constantExpression(name()).evaluate(exchange, type);
064        }
065    
066    }