Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.downloader / org.gvsig.downloader.lib / org.gvsig.downloader.lib.impl / src / main / java / org / gvsig / downloader / lib / impl / types / httpbasic / DownloaderHTTPBasicCredentials.java @ 47845

History | View | Annotate | Download (2.44 KB)

1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.downloader.lib.impl.types.httpbasic;
7

    
8
import java.nio.charset.StandardCharsets;
9
import java.util.Base64;
10
import org.apache.commons.lang3.StringUtils;
11
import org.gvsig.downloader.DownloaderAuthenticationConfig;
12
import org.gvsig.downloader.DownloaderCredentials;
13
import org.gvsig.downloader.DownloaderManager;
14
import static org.gvsig.downloader.lib.impl.types.httpbasic.DownloaderHTTPBasicAuthenticationFactory.HTTPBASIC_AUTHENTICATION_NAME;
15
import org.gvsig.downloader.spi.AbstractDownloaderCredentials;
16

    
17
/**
18
 *
19
 * @author jjdelcerro
20
 */
21
public class DownloaderHTTPBasicCredentials 
22
    extends 
23
        AbstractDownloaderCredentials
24
    {
25
    
26
    private final String userid;
27
    private final String password;
28

    
29
    public DownloaderHTTPBasicCredentials(String serviceUrl, String userid, String password) {
30
        super(HTTPBASIC_AUTHENTICATION_NAME, serviceUrl);
31
        this.userid = userid;
32
        this.password = password;
33
    }
34

    
35
    @Override
36
    public String getAuthorizationToken() {
37
        return "Basic "+Base64.getEncoder().encodeToString((this.userid+":"+this.password).getBytes(StandardCharsets.UTF_8));
38
    }
39

    
40
    @Override
41
    public String getUserid() {
42
        return this.userid;
43
    }
44

    
45
    public String getPassword() {
46
        return password;
47
    }
48

    
49
    @Override
50
    public DownloaderCredentials createCredentials(String serviceUrl) {
51
        if (StringUtils.isBlank(serviceUrl)) {
52
            return null;
53
        }
54
        
55
        return new DownloaderHTTPBasicCredentials(
56
            serviceUrl,
57
            this.getUserid(),
58
            this.getPassword()
59
        );
60
    }
61

    
62
    @Override
63
    public DownloaderCredentials createCredentials(DownloaderAuthenticationConfig config) {
64
        if(!(config instanceof DownloaderHTTPBasicAuthenticationConfig)){
65
            return null;
66
        }
67
        DownloaderHTTPBasicAuthenticationConfig otherConfig = (DownloaderHTTPBasicAuthenticationConfig)config;
68
        if (DownloaderManager.urlStartsWith(otherConfig.getServiceUrl(), this.getServiceUrl())) {
69
                return new DownloaderHTTPBasicCredentials(
70
                        otherConfig.getServiceUrl(),
71
                        this.getUserid(), 
72
                        this.getPassword()
73
                );
74
        }
75
        return null;
76
    }
77

    
78

    
79
    
80
}