org.nuxeo.ecm.automation.client.jaxrs.spi.auth.BasicAuthInterceptor.java Source code

Java tutorial

Introduction

Here is the source code for org.nuxeo.ecm.automation.client.jaxrs.spi.auth.BasicAuthInterceptor.java

Source

/*
 * (C) Copyright 2006-2011 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     bstefanescu
 */
package org.nuxeo.ecm.automation.client.jaxrs.spi.auth;

import org.apache.http.HttpRequest;
import org.nuxeo.ecm.automation.client.jaxrs.RequestInterceptor;
import org.nuxeo.ecm.automation.client.jaxrs.spi.Connector;
import org.nuxeo.ecm.automation.client.jaxrs.spi.Request;
import org.nuxeo.ecm.automation.client.jaxrs.util.Base64;

/**
 * Inject the basic authentication header in the request
 * 
 * @author matic
 * 
 */
public class BasicAuthInterceptor implements RequestInterceptor {

    protected String token;

    public BasicAuthInterceptor(String username, String password) {
        setAuth(username, password);
    }

    public void setAuth(String username, String password) {
        String info = username.concat(":").concat(password);
        token = "Basic " + Base64.encode(info);
    }

    @Override
    public void processRequest(Request request, Connector connector) {
        request.put("Authorization", token);
    }

    @Override
    public void processHttpRequest(HttpRequest request) {
        request.addHeader("Authorization", token);
    }

}