mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-01 19:15:59 +01:00
Use Apache HttpComponents instead of URL.openStream
This commit is contained in:
@@ -6,8 +6,8 @@ import java.nio.file.{Files, Paths, StandardWatchEventKinds}
|
||||
import java.util.Base64
|
||||
import java.util.concurrent.ConcurrentLinkedQueue
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import javax.servlet.ServletContext
|
||||
|
||||
import javax.servlet.ServletContext
|
||||
import com.github.zafarkhaja.semver.Version
|
||||
import gitbucket.core.controller.{Context, ControllerBase}
|
||||
import gitbucket.core.model.{Account, Issue}
|
||||
@@ -18,10 +18,12 @@ import gitbucket.core.service.SystemSettingsService.SystemSettings
|
||||
import gitbucket.core.util.SyntaxSugars._
|
||||
import gitbucket.core.util.DatabaseConfig
|
||||
import gitbucket.core.util.Directory._
|
||||
import gitbucket.core.util.HttpClientUtil._
|
||||
import io.github.gitbucket.solidbase.Solidbase
|
||||
import io.github.gitbucket.solidbase.manager.JDBCVersionManager
|
||||
import io.github.gitbucket.solidbase.model.Module
|
||||
import org.apache.commons.io.FileUtils
|
||||
import org.apache.http.client.methods.HttpGet
|
||||
import org.apache.sshd.server.Command
|
||||
import org.slf4j.LoggerFactory
|
||||
import play.twirl.api.Html
|
||||
@@ -253,8 +255,17 @@ object PluginRegistry {
|
||||
})
|
||||
.foreach(_.delete())
|
||||
|
||||
val in = url.openStream()
|
||||
FileUtils.copyToFile(in, new File(PluginHome, new File(url.getFile).getName))
|
||||
withHttpClient(settings.proxy) { httpClient =>
|
||||
val httpGet = new HttpGet(url.toString)
|
||||
try {
|
||||
val response = httpClient.execute(httpGet)
|
||||
val in = response.getEntity.getContent
|
||||
FileUtils.copyToFile(in, new File(PluginHome, new File(url.getFile).getName))
|
||||
} finally {
|
||||
httpGet.releaseConnection()
|
||||
}
|
||||
}
|
||||
|
||||
instance = new PluginRegistry()
|
||||
initialize(context, settings, conn)
|
||||
}
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
package gitbucket.core.plugin
|
||||
|
||||
import java.net.{InetSocketAddress, PasswordAuthentication}
|
||||
import java.net.{Proxy => JProxy}
|
||||
|
||||
import gitbucket.core.controller.Context
|
||||
import gitbucket.core.util.SyntaxSugars.using
|
||||
import gitbucket.core.util.HttpClientUtil._
|
||||
import org.json4s._
|
||||
import org.apache.commons.io.IOUtils
|
||||
|
||||
import java.net.Authenticator
|
||||
import org.apache.http.client.methods.HttpGet
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
object PluginRepository {
|
||||
@@ -22,23 +20,18 @@ object PluginRepository {
|
||||
def getPlugins()(implicit context: Context): Seq[PluginMetadata] = {
|
||||
try {
|
||||
val url = new java.net.URL("https://plugins.gitbucket-community.org/releases/plugins.json")
|
||||
using(context.settings.proxy match {
|
||||
case Some(proxy) =>
|
||||
(proxy.user, proxy.password) match {
|
||||
case (Some(user), Some(password)) =>
|
||||
Authenticator.setDefault(new Authenticator() {
|
||||
override def getPasswordAuthentication = new PasswordAuthentication(user, password.toCharArray)
|
||||
})
|
||||
case _ =>
|
||||
Authenticator.setDefault(null)
|
||||
|
||||
withHttpClient(context.settings.proxy) { httpClient =>
|
||||
val httpGet = new HttpGet(url.toString)
|
||||
try {
|
||||
val response = httpClient.execute(httpGet)
|
||||
using(response.getEntity.getContent) { in =>
|
||||
val str = IOUtils.toString(in, "UTF-8")
|
||||
parsePluginJson(str)
|
||||
}
|
||||
url
|
||||
.openConnection(new JProxy(JProxy.Type.HTTP, new InetSocketAddress(proxy.host, proxy.port)))
|
||||
.getInputStream
|
||||
case None => url.openStream()
|
||||
}) { in =>
|
||||
val str = IOUtils.toString(in, "UTF-8")
|
||||
parsePluginJson(str)
|
||||
} finally {
|
||||
httpGet.releaseConnection()
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
case t: Throwable =>
|
||||
|
||||
34
src/main/scala/gitbucket/core/util/HttpClientUtil.scala
Normal file
34
src/main/scala/gitbucket/core/util/HttpClientUtil.scala
Normal file
@@ -0,0 +1,34 @@
|
||||
package gitbucket.core.util
|
||||
|
||||
import gitbucket.core.service.SystemSettingsService
|
||||
import org.apache.http.HttpHost
|
||||
import org.apache.http.auth.{AuthScope, UsernamePasswordCredentials}
|
||||
import org.apache.http.impl.client.{BasicCredentialsProvider, CloseableHttpClient, HttpClientBuilder}
|
||||
|
||||
object HttpClientUtil {
|
||||
|
||||
def withHttpClient[T](proxy: Option[SystemSettingsService.Proxy])(f: CloseableHttpClient => T): T = {
|
||||
val builder = HttpClientBuilder.create.useSystemProperties
|
||||
|
||||
proxy.foreach { proxy =>
|
||||
for (user <- proxy.user; password <- proxy.password) {
|
||||
builder.setProxy(new HttpHost(proxy.host, proxy.port))
|
||||
val credential = new BasicCredentialsProvider()
|
||||
credential.setCredentials(
|
||||
new AuthScope(proxy.host, proxy.port),
|
||||
new UsernamePasswordCredentials(user, password)
|
||||
)
|
||||
builder.setDefaultCredentialsProvider(credential)
|
||||
}
|
||||
}
|
||||
|
||||
val httpClient = builder.build()
|
||||
|
||||
try {
|
||||
f(httpClient)
|
||||
} finally {
|
||||
httpClient.close()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user