ref #6190 removing svn property place holder in first line of code - java files
[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.commons.httpclient.methods.PostMethod;
15 import org.apache.http.client.methods.HttpPost;
16 import org.springframework.remoting.httpinvoker.HttpComponentsHttpInvokerRequestExecutor;
17 import org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration;
18 import org.springframework.security.core.Authentication;
19 import org.springframework.security.core.context.SecurityContext;
20
21 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
22
23 /**
24 * @author cmathew
25 * @date 27 Jan 2015
26 *
27 */
28 public class CdmAuthenticatedHttpInvokerRequestExecutor extends HttpComponentsHttpInvokerRequestExecutor {
29
30 @Override
31 protected HttpPost createHttpPost(HttpInvokerClientConfiguration config) throws IOException {
32 HttpPost postMethod = super.createHttpPost(config);
33
34 SecurityContext securityContext = CdmApplicationState.getCurrentSecurityContext();
35 if(securityContext != null) {
36 Authentication auth = securityContext.getAuthentication();
37 if ((auth != null) && (auth.getName() != null) &&
38 (auth.getCredentials() != null)) {
39 String base64 = auth.getName() + ":" + auth.getCredentials().toString();
40 postMethod.setHeader("Authorization", "Basic " +
41 new String(Base64.encodeBase64(base64.getBytes())));
42 }
43 }
44 return postMethod;
45 }
46 }