001/** 002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package io.jboot.components.http; 017 018import io.jboot.utils.StrUtil; 019 020import java.io.Serializable; 021import java.net.Authenticator; 022import java.net.InetSocketAddress; 023import java.net.PasswordAuthentication; 024import java.net.Proxy; 025 026import static java.net.Proxy.Type.HTTP; 027 028public class HttpProxyInfo implements Serializable { 029 030 private String proxyHost; 031 private Integer proxyPort; 032 private String proxyUser; 033 private String proxyPassword; 034 035 private Proxy proxy; 036 037 public HttpProxyInfo() { 038 } 039 040 public HttpProxyInfo(String proxyHost, Integer proxyPort) { 041 this.proxyHost = proxyHost; 042 this.proxyPort = proxyPort; 043 } 044 045 public HttpProxyInfo(String proxyHost, Integer proxyPort, String proxyUser, String proxyPassword) { 046 this.proxyHost = proxyHost; 047 this.proxyPort = proxyPort; 048 this.proxyUser = proxyUser; 049 this.proxyPassword = proxyPassword; 050 setAuthenticator(); 051 } 052 053 public void setAuthenticator() { 054 if (StrUtil.isNotBlank(proxyUser) && StrUtil.isNotBlank(proxyPassword)) { 055 Authenticator.setDefault(new Authenticator() { 056 @Override 057 protected PasswordAuthentication getPasswordAuthentication() { 058 return new PasswordAuthentication(proxyUser, proxyPassword.toCharArray()); 059 } 060 }); 061 } 062 } 063 064 public String getProxyHost() { 065 return proxyHost; 066 } 067 068 public void setProxyHost(String proxyHost) { 069 this.proxyHost = proxyHost; 070 } 071 072 public Integer getProxyPort() { 073 return proxyPort; 074 } 075 076 public void setProxyPort(Integer proxyPort) { 077 this.proxyPort = proxyPort; 078 } 079 080 public String getProxyUser() { 081 return proxyUser; 082 } 083 084 public void setProxyUser(String proxyUser) { 085 this.proxyUser = proxyUser; 086 } 087 088 public String getProxyPassword() { 089 return proxyPassword; 090 } 091 092 public void setProxyPassword(String proxyPassword) { 093 this.proxyPassword = proxyPassword; 094 } 095 096 097 public Proxy getProxy() { 098 if (proxy == null) { 099 proxy = new Proxy(HTTP, new InetSocketAddress(getProxyHost(), getProxyPort())); 100 } 101 return proxy; 102 } 103}