org.overlord.security.eval.webapp1.services.JaxrsService.java Source code

Java tutorial

Introduction

Here is the source code for org.overlord.security.eval.webapp1.services.JaxrsService.java

Source

/*
 * Copyright 2013 JBoss Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.overlord.security.eval.webapp1.services;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.DefaultHttpClient;
import org.jboss.resteasy.client.ClientExecutor;
import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;
import org.jboss.resteasy.client.core.executors.ApacheHttpClient4Executor;

/**
 * @author eric.wittmann@redhat.com
 */
public class JaxrsService {

    private static final String ENDPOINT = "http://localhost:8080/security-eval-jaxrs/greeting";
    private static final String USER = "impersonator";
    private static final String PASS = "secure";

    private HttpServletRequest context;

    /**
     * Constructor.
     */
    public JaxrsService(HttpServletRequest context) {
        this.context = context;
    }

    /**
     * @return
     */
    public String doGreeting() {
        try {
            ClientRequest request = new ClientRequest(ENDPOINT, getBasicAuthExecutor());
            ClientResponse<String> response = request.get(String.class);
            int status = response.getStatus();
            System.out.println("Greeting status: " + status);
            return response.getEntity();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * @return
     */
    private ClientExecutor getBasicAuthExecutor() {
        String password = PASS + "||" + this.context.getRemoteUser();
        Credentials credentials = new UsernamePasswordCredentials(USER, password);
        DefaultHttpClient httpClient = new DefaultHttpClient();
        httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, credentials);
        ClientExecutor clientExecutor = new ApacheHttpClient4Executor(httpClient);
        return clientExecutor;
    }

}