adapt dependencies, not finished yet
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / taxeditor / service / CdmAuthenticatedHttpInvokerRequestExecutor.java
1 // $Id$
2 /**
3 * Copyright (C) 2015 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.taxeditor.service;
11
12 import java.io.IOException;
13
14 import org.apache.commons.codec.binary.Base64;
15 import org.apache.commons.httpclient.methods.PostMethod;
16 import org.apache.http.client.methods.HttpPost;
17 import org.springframework.remoting.httpinvoker.HttpComponentsHttpInvokerRequestExecutor;
18 import org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration;
19 import org.springframework.security.core.Authentication;
20 import org.springframework.security.core.context.SecurityContext;
21
22 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
23
24 /**
25 * @author cmathew
26 * @date 27 Jan 2015
27 *
28 */
29 public class CdmAuthenticatedHttpInvokerRequestExecutor extends HttpComponentsHttpInvokerRequestExecutor {
30
31 @Override
32 protected HttpPost createHttpPost(HttpInvokerClientConfiguration config) throws IOException {
33 HttpPost postMethod = super.createHttpPost(config);
34
35 SecurityContext securityContext = CdmApplicationState.getCurrentSecurityContext();
36 if(securityContext != null) {
37 Authentication auth = securityContext.getAuthentication();
38 if ((auth != null) && (auth.getName() != null) &&
39 (auth.getCredentials() != null)) {
40 String base64 = auth.getName() + ":" + auth.getCredentials().toString();
41 postMethod.setHeader("Authorization", "Basic " +
42 new String(Base64.encodeBase64(base64.getBytes())));
43 }
44 }
45 return postMethod;
46 }
47 }