/*
* (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and others.
*
* 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.
*
* Contributors:
* Antoine Taillefer
*/
package org.nuxeo.ecm.automation.client.jaxrs.spi.auth;
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.spi.RequestInterceptor;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientRequest;
import com.sun.jersey.api.client.ClientResponse;
/**
* Injects the token authentication header in the request.
*
* @author Antoine Taillefer (ataillefer@nuxeo.com)
* @since 5.7
*/
public class TokenAuthInterceptor extends RequestInterceptor {
protected static final String TOKEN_HEADER = "X-Authentication-Token";
protected String token;
public TokenAuthInterceptor(String token) {
this.token = token;
}
@Override
public void processRequest(Request request, Connector connector) {
request.put(TOKEN_HEADER, token);
}
@Override
public ClientResponse handle(ClientRequest cr) throws ClientHandlerException {
if (!cr.getHeaders().containsKey(TOKEN_HEADER)) {
cr.getHeaders().add(TOKEN_HEADER, token);
}
return getNext().handle(cr);
}
}