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.hbase.http.resource;
019
020import java.io.IOException;
021import java.util.Map;
022import java.util.TreeMap;
023import javax.ws.rs.DefaultValue;
024import javax.ws.rs.GET;
025import javax.ws.rs.Path;
026import javax.ws.rs.PathParam;
027import javax.ws.rs.Produces;
028import javax.ws.rs.QueryParam;
029import javax.ws.rs.core.MediaType;
030import javax.ws.rs.core.Response;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034import org.apache.hbase.thirdparty.org.eclipse.jetty.util.ajax.JSON;
035
036/**
037 * A simple Jersey resource class TestHttpServer.
038 * The servlet simply puts the path and the op parameter in a map
039 * and return it in JSON format in the response.
040 */
041@Path("")
042public class JerseyResource {
043  private static final Logger LOG = LoggerFactory.getLogger(JerseyResource.class);
044
045  public static final String PATH = "path";
046  public static final String OP = "op";
047
048  @GET
049  @Path("{" + PATH + ":.*}")
050  @Produces({MediaType.APPLICATION_JSON})
051  public Response get(
052      @PathParam(PATH) @DefaultValue("UNKNOWN_" + PATH) final String path,
053      @QueryParam(OP) @DefaultValue("UNKNOWN_" + OP) final String op
054  ) throws IOException {
055    LOG.info("get: " + PATH + "=" + path + ", " + OP + "=" + op);
056
057    final Map<String, Object> m = new TreeMap<>();
058    m.put(PATH, path);
059    m.put(OP, op);
060    final String js = JSON.toString(m);
061    return Response.ok(js).type(MediaType.APPLICATION_JSON).build();
062  }
063}