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