Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
apollo
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
openSource
apollo
Commits
2d0b0e8a
Commit
2d0b0e8a
authored
Apr 19, 2016
by
Jason Song
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #109 from yiming187/rest_factory
Support auth in RestTemplateFactory
parents
fd08a717
f36d948c
Hide whitespace changes
Inline
Side-by-side
Showing
18 changed files
with
109 additions
and
29 deletions
+109
-29
AppController.java
...m/ctrip/apollo/adminservice/controller/AppController.java
+1
-1
ClusterController.java
...rip/apollo/adminservice/controller/ClusterController.java
+1
-1
ItemController.java
.../ctrip/apollo/adminservice/controller/ItemController.java
+1
-1
ItemSetController.java
...rip/apollo/adminservice/controller/ItemSetController.java
+1
-1
NamespaceController.java
...p/apollo/adminservice/controller/NamespaceController.java
+1
-1
ReleaseController.java
...rip/apollo/adminservice/controller/ReleaseController.java
+1
-1
HttpUtil.java
...nt/src/main/java/com/ctrip/apollo/util/http/HttpUtil.java
+17
-9
pom.xml
apollo-common/pom.xml
+4
-0
ActiveUser.java
...rc/main/java/com/ctrip/apollo/common/auth/ActiveUser.java
+1
-1
RestTemplateFactory.java
...ava/com/ctrip/apollo/common/auth/RestTemplateFactory.java
+41
-0
pom.xml
apollo-configservice/pom.xml
+5
-0
ConfigServiceTestConfiguration.java
...java/com/ctrip/apollo/ConfigServiceTestConfiguration.java
+3
-1
PortalApplication.java
...tal/src/main/java/com/ctrip/apollo/PortalApplication.java
+1
-1
API.java
...portal/src/main/java/com/ctrip/apollo/portal/api/API.java
+16
-4
application.yml
apollo-portal/src/main/resources/application.yml
+1
-1
SamplePortalApplication.java
...c/test/java/com/ctrip/apollo/SamplePortalApplication.java
+11
-0
AbstractPortalTest.java
...test/java/com/ctrip/apollo/portal/AbstractPortalTest.java
+2
-0
application.yml
apollo-portal/src/test/resources/application.yml
+1
-6
No files found.
apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/AppController.java
View file @
2d0b0e8a
...
...
@@ -17,7 +17,7 @@ import org.springframework.web.bind.annotation.RestController;
import
com.ctrip.apollo.biz.entity.App
;
import
com.ctrip.apollo.biz.service.AdminService
;
import
com.ctrip.apollo.biz.service.AppService
;
import
com.ctrip.apollo.common.
controller
.ActiveUser
;
import
com.ctrip.apollo.common.
auth
.ActiveUser
;
import
com.ctrip.apollo.common.utils.BeanUtils
;
import
com.ctrip.apollo.core.dto.AppDTO
;
import
com.ctrip.apollo.core.exception.NotFoundException
;
...
...
apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/ClusterController.java
View file @
2d0b0e8a
...
...
@@ -15,7 +15,7 @@ import org.springframework.web.bind.annotation.RestController;
import
com.ctrip.apollo.biz.entity.Cluster
;
import
com.ctrip.apollo.biz.service.ClusterService
;
import
com.ctrip.apollo.biz.service.ViewService
;
import
com.ctrip.apollo.common.
controller
.ActiveUser
;
import
com.ctrip.apollo.common.
auth
.ActiveUser
;
import
com.ctrip.apollo.common.utils.BeanUtils
;
import
com.ctrip.apollo.core.dto.ClusterDTO
;
import
com.ctrip.apollo.core.exception.NotFoundException
;
...
...
apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/ItemController.java
View file @
2d0b0e8a
...
...
@@ -15,7 +15,7 @@ import org.springframework.web.bind.annotation.RestController;
import
com.ctrip.apollo.biz.entity.Item
;
import
com.ctrip.apollo.biz.service.ItemService
;
import
com.ctrip.apollo.biz.service.ViewService
;
import
com.ctrip.apollo.common.
controller
.ActiveUser
;
import
com.ctrip.apollo.common.
auth
.ActiveUser
;
import
com.ctrip.apollo.common.utils.BeanUtils
;
import
com.ctrip.apollo.core.dto.ItemDTO
;
import
com.ctrip.apollo.core.exception.NotFoundException
;
...
...
apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/ItemSetController.java
View file @
2d0b0e8a
...
...
@@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
import
org.springframework.web.bind.annotation.RestController
;
import
com.ctrip.apollo.biz.service.ItemSetService
;
import
com.ctrip.apollo.common.
controller
.ActiveUser
;
import
com.ctrip.apollo.common.
auth
.ActiveUser
;
import
com.ctrip.apollo.core.dto.ItemChangeSets
;
@RestController
...
...
apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/NamespaceController.java
View file @
2d0b0e8a
...
...
@@ -15,7 +15,7 @@ import org.springframework.web.bind.annotation.RestController;
import
com.ctrip.apollo.biz.entity.Namespace
;
import
com.ctrip.apollo.biz.service.NamespaceService
;
import
com.ctrip.apollo.biz.service.ViewService
;
import
com.ctrip.apollo.common.
controller
.ActiveUser
;
import
com.ctrip.apollo.common.
auth
.ActiveUser
;
import
com.ctrip.apollo.common.utils.BeanUtils
;
import
com.ctrip.apollo.core.dto.NamespaceDTO
;
import
com.ctrip.apollo.core.exception.NotFoundException
;
...
...
apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/ReleaseController.java
View file @
2d0b0e8a
...
...
@@ -14,7 +14,7 @@ import com.ctrip.apollo.biz.entity.Release;
import
com.ctrip.apollo.biz.service.ConfigService
;
import
com.ctrip.apollo.biz.service.ReleaseService
;
import
com.ctrip.apollo.biz.service.ViewService
;
import
com.ctrip.apollo.common.
controller
.ActiveUser
;
import
com.ctrip.apollo.common.
auth
.ActiveUser
;
import
com.ctrip.apollo.common.utils.BeanUtils
;
import
com.ctrip.apollo.core.dto.ReleaseDTO
;
import
com.ctrip.apollo.core.exception.NotFoundException
;
...
...
apollo-client/src/main/java/com/ctrip/apollo/util/http/HttpUtil.java
View file @
2d0b0e8a
...
...
@@ -2,6 +2,7 @@ package com.ctrip.apollo.util.http;
import
com.google.common.base.Charsets
;
import
com.google.common.base.Function
;
import
com.google.common.io.BaseEncoding
;
import
com.google.gson.Gson
;
import
com.ctrip.apollo.util.ConfigUtil
;
...
...
@@ -12,6 +13,7 @@ import org.unidal.lookup.annotation.Named;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.UnsupportedEncodingException
;
import
java.lang.reflect.Type
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
...
...
@@ -24,15 +26,21 @@ public class HttpUtil {
@Inject
private
ConfigUtil
m_configUtil
;
private
Gson
gson
;
private
String
basicAuth
;
public
HttpUtil
()
{
gson
=
new
Gson
();
try
{
basicAuth
=
"Basic "
+
BaseEncoding
.
base64
().
encode
(
""
.
getBytes
(
"UTF-8"
));
}
catch
(
UnsupportedEncodingException
e
)
{
e
.
printStackTrace
();
}
}
/**
* Do get operation for the http request.
*
* @param httpRequest
the request
* @param httpRequest the request
* @param responseType the response type
* @return the response
* @throws RuntimeException if any error happened or response code is neither 200 nor 304
...
...
@@ -51,7 +59,7 @@ public class HttpUtil {
/**
* Do get operation for the http request.
*
* @param httpRequest
the request
* @param httpRequest the request
* @param responseType the response type
* @return the response
* @throws RuntimeException if any error happened or response code is neither 200 nor 304
...
...
@@ -68,13 +76,14 @@ public class HttpUtil {
}
private
<
T
>
HttpResponse
<
T
>
doGetWithSerializeFunction
(
HttpRequest
httpRequest
,
Function
<
String
,
T
>
serializeFunction
)
{
Function
<
String
,
T
>
serializeFunction
)
{
InputStream
is
=
null
;
try
{
HttpURLConnection
conn
=
(
HttpURLConnection
)
new
URL
(
httpRequest
.
getUrl
()).
openConnection
();
conn
.
setRequestMethod
(
"GET"
);
conn
.
setRequestProperty
(
"Authorization"
,
basicAuth
);
int
connectTimeout
=
httpRequest
.
getConnectTimeout
();
if
(
connectTimeout
<
0
)
{
connectTimeout
=
m_configUtil
.
getConnectTimeout
();
...
...
@@ -102,9 +111,8 @@ public class HttpUtil {
return
new
HttpResponse
<>(
statusCode
,
null
);
}
throw
new
RuntimeException
(
String
.
format
(
"Get operation failed for %s, status code - %d"
,
httpRequest
.
getUrl
(),
statusCode
));
throw
new
RuntimeException
(
String
.
format
(
"Get operation failed for %s, status code - %d"
,
httpRequest
.
getUrl
(),
statusCode
));
}
catch
(
Throwable
ex
)
{
throw
new
RuntimeException
(
"Could not complete get operation"
,
ex
);
...
...
@@ -113,7 +121,7 @@ public class HttpUtil {
try
{
is
.
close
();
}
catch
(
IOException
ex
)
{
//ignore
//
ignore
}
}
}
...
...
apollo-common/pom.xml
View file @
2d0b0e8a
...
...
@@ -39,6 +39,10 @@
<artifactId>
spring-data-commons
</artifactId>
</dependency>
<dependency>
<groupId>
org.apache.httpcomponents
</groupId>
<artifactId>
httpclient
</artifactId>
</dependency>
<dependency>
<groupId>
ch.qos.logback
</groupId>
<artifactId>
logback-classic
</artifactId>
</dependency>
...
...
apollo-common/src/main/java/com/ctrip/apollo/common/
controller
/ActiveUser.java
→
apollo-common/src/main/java/com/ctrip/apollo/common/
auth
/ActiveUser.java
View file @
2d0b0e8a
package
com
.
ctrip
.
apollo
.
common
.
controller
;
package
com
.
ctrip
.
apollo
.
common
.
auth
;
import
java.lang.annotation.Documented
;
import
java.lang.annotation.ElementType
;
...
...
apollo-common/src/main/java/com/ctrip/apollo/common/auth/RestTemplateFactory.java
0 → 100644
View file @
2d0b0e8a
package
com
.
ctrip
.
apollo
.
common
.
auth
;
import
org.apache.http.auth.AuthScope
;
import
org.apache.http.auth.UsernamePasswordCredentials
;
import
org.apache.http.impl.client.BasicCredentialsProvider
;
import
org.apache.http.impl.client.CloseableHttpClient
;
import
org.apache.http.impl.client.HttpClientBuilder
;
import
org.springframework.beans.factory.FactoryBean
;
import
org.springframework.beans.factory.InitializingBean
;
import
org.springframework.http.client.HttpComponentsClientHttpRequestFactory
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.client.RestTemplate
;
@Component
public
class
RestTemplateFactory
implements
FactoryBean
<
RestTemplate
>,
InitializingBean
{
private
RestTemplate
restTemplate
;
public
RestTemplate
getObject
()
{
return
restTemplate
;
}
public
Class
<
RestTemplate
>
getObjectType
()
{
return
RestTemplate
.
class
;
}
public
boolean
isSingleton
()
{
return
true
;
}
public
void
afterPropertiesSet
()
{
BasicCredentialsProvider
credentialsProvider
=
new
BasicCredentialsProvider
();
credentialsProvider
.
setCredentials
(
AuthScope
.
ANY
,
new
UsernamePasswordCredentials
(
"apollo"
,
""
));
CloseableHttpClient
httpClient
=
HttpClientBuilder
.
create
().
setDefaultCredentialsProvider
(
credentialsProvider
).
build
();
restTemplate
=
new
RestTemplate
(
new
HttpComponentsClientHttpRequestFactory
(
httpClient
));
}
}
apollo-configservice/pom.xml
View file @
2d0b0e8a
...
...
@@ -67,6 +67,11 @@
</exclusions>
</dependency>
<!-- end of eureka -->
<dependency>
<groupId>
com.h2database
</groupId>
<artifactId>
h2
</artifactId>
<scope>
test
</scope>
</dependency>
</dependencies>
<build>
<plugins>
...
...
apollo-configservice/src/test/java/com/ctrip/apollo/ConfigServiceTestConfiguration.java
View file @
2d0b0e8a
...
...
@@ -6,9 +6,11 @@ import org.springframework.context.annotation.ComponentScan.Filter;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.FilterType
;
import
com.ctrip.apollo.common.controller.WebSecurityConfig
;
@Configuration
@ComponentScan
(
excludeFilters
=
{
@Filter
(
type
=
FilterType
.
ASSIGNABLE_TYPE
,
value
=
{
SampleConfigServiceApplication
.
class
,
ConfigServiceApplication
.
class
})})
SampleConfigServiceApplication
.
class
,
ConfigServiceApplication
.
class
,
WebSecurityConfig
.
class
})})
@EnableAutoConfiguration
public
class
ConfigServiceTestConfiguration
{
...
...
apollo-portal/src/main/java/com/ctrip/apollo/
portal/
PortalApplication.java
→
apollo-portal/src/main/java/com/ctrip/apollo/PortalApplication.java
View file @
2d0b0e8a
package
com
.
ctrip
.
apollo
.
portal
;
package
com
.
ctrip
.
apollo
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.actuate.system.ApplicationPidFileWriter
;
...
...
apollo-portal/src/main/java/com/ctrip/apollo/portal/api/API.java
View file @
2d0b0e8a
package
com
.
ctrip
.
apollo
.
portal
.
api
;
import
com.ctrip.apollo.core.enums.Env
;
import
com.ctrip.apollo.core.exception.ServiceException
;
import
com.ctrip.apollo.portal.service.ServiceLocator
;
import
javax.annotation.PostConstruct
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.client.RestTemplate
;
import
com.ctrip.apollo.common.auth.RestTemplateFactory
;
import
com.ctrip.apollo.core.enums.Env
;
import
com.ctrip.apollo.core.exception.ServiceException
;
import
com.ctrip.apollo.portal.service.ServiceLocator
;
public
class
API
{
@Autowired
protected
ServiceLocator
serviceLocator
;
protected
RestTemplate
restTemplate
=
new
RestTemplate
();
@Autowired
private
RestTemplateFactory
restTemplateFactory
;
protected
RestTemplate
restTemplate
;
@PostConstruct
private
void
postConstruct
()
{
restTemplate
=
restTemplateFactory
.
getObject
();
}
public
String
getAdminServiceHost
(
Env
env
)
{
// 本地测试用
...
...
@@ -24,4 +35,5 @@ public class API {
}
return
""
;
}
}
apollo-portal/src/main/resources/application.yml
View file @
2d0b0e8a
...
...
@@ -15,4 +15,4 @@ ctrip:
apollo
:
portal
:
env
:
dev
env
:
local
apollo-portal/src/test/java/com/ctrip/apollo/SamplePortalApplication.java
0 → 100644
View file @
2d0b0e8a
package
com
.
ctrip
.
apollo
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.boot.builder.SpringApplicationBuilder
;
@SpringBootApplication
public
class
SamplePortalApplication
{
public
static
void
main
(
String
[]
args
)
{
new
SpringApplicationBuilder
(
SamplePortalApplication
.
class
).
run
(
args
);
}
}
apollo-portal/src/test/java/com/ctrip/apollo/portal/AbstractPortalTest.java
View file @
2d0b0e8a
...
...
@@ -4,6 +4,8 @@ import org.junit.runner.RunWith;
import
org.springframework.boot.test.SpringApplicationConfiguration
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
com.ctrip.apollo.PortalApplication
;
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@SpringApplicationConfiguration
(
classes
=
PortalApplication
.
class
)
public
abstract
class
AbstractPortalTest
{
...
...
apollo-portal/src/test/resources/application.yml
View file @
2d0b0e8a
...
...
@@ -4,11 +4,6 @@ server:
spring
:
application
:
name
:
apollo-portal
datasource
:
url
:
jdbc:h2:mem:~/fxapolloportaldb
jpa
:
hibernate
:
naming_strategy
:
org.hibernate.cfg.EJB3NamingStrategy
logging
:
level
:
...
...
@@ -20,4 +15,4 @@ ctrip:
apollo
:
portal
:
env
:
dev
env
:
local
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment