@ -4,6 +4,7 @@ import com.fanruan.workplace.http.HttpConstants;
import com.fanruan.workplace.http.HttpServiceMap ;
import com.fanruan.workplace.http.RepositoryManager ;
import com.fanruan.workplace.http.info.Stub ;
import com.fanruan.workplace.network.RemoteNetworkRepository ;
import com.fr.log.FineLoggerFactory ;
import com.fr.stable.StringUtils ;
import com.fr.workspace.engine.exception.RemoteDesignConnectionException ;
@ -43,33 +44,20 @@ public class HttpWorkspaceConnector implements WorkspaceConnector {
@Override
public boolean testConnection ( WorkspaceConnectionInfo connectionInfo ) throws Exception {
FineWorkspaceHttpClient client = FineWorkspaceHttpClient . create ( "Http-Client" , connectionInfo ) ;
LoginResponseInfoBean bean ;
try {
bean = RemoteAuthorityRepository . getInstance ( ) . login ( createLoginBean ( connectionInfo ) ) ;
checkValidAndUpdateInfo ( client , connectionInfo , bean ) ;
return testConnect ( client , connectionInfo ) ;
} finally {
client . closePool ( ) ;
}
return StringUtils . isNotEmpty ( bean . getAccessToken ( ) ) ;
}
@Override
public WorkspaceClient connect ( WorkspaceConnectionInfo connectionInfo ) throws Exception {
FineWorkspaceHttpClient client = FineWorkspaceHttpClient . create ( "Http-Client" , connectionInfo ) ;
try {
for ( Map . Entry < Class , Stub < ? > > entry : HttpServiceMap . getInstance ( ) . entrySet ( ) ) {
Stub < ? > stub = entry . getValue ( ) ;
stub . clearProxy ( ) ;
}
LoginResponseInfoBean bean = RemoteAuthorityRepository . getInstance ( ) . login ( createLoginBean ( connectionInfo ) ) ;
// 服务器如果没返回token,肯定是出问题了,直接抛出异常
if ( StringUtils . isEmpty ( bean . getAccessToken ( ) ) ) {
throw new RuntimeException ( "Empty token, try connect again and check server" ) ;
if ( testConnect ( client , connectionInfo ) ) {
client . connect ( ) ;
}
FineLoggerFactory . getLogger ( ) . info ( "[connector] infos:{}" , bean . getAccessToken ( ) ) ;
checkValidAndUpdateInfo ( client , connectionInfo , bean ) ;
client . startHeartBeat ( ) ;
} catch ( Exception e ) {
client . closePool ( ) ;
throw e ;
@ -79,28 +67,25 @@ public class HttpWorkspaceConnector implements WorkspaceConnector {
return client ;
}
private void checkValidAndUpdateInfo ( FineWorkspaceHttpClient client , WorkspaceConnectionInfo connectionInfo , LoginResponseInfoBean bean ) throws Exception {
private boolean testConnect ( FineWorkspaceHttpClient client , WorkspaceConnectionInfo connectionInfo ) throws Exception {
for ( Map . Entry < Class , Stub < ? > > entry : HttpServiceMap . getInstance ( ) . entrySet ( ) ) {
Stub < ? > stub = entry . getValue ( ) ;
stub . clearProxy ( ) ;
}
LoginResponseInfoBean bean = RemoteAuthorityRepository . getInstance ( ) . login ( createLoginBean ( connectionInfo ) ) ;
WorkspaceConnection connection = new WorkspaceConnection (
UUID . randomUUID ( ) . toString ( ) ,
connectionInfo . getUserName ( ) ,
HttpConstants . AUTHORIZATION_PREFIX + bean . getAccessToken ( ) ,
InetAddress . getLocalHost ( ) . getHostAddress ( ) ) ;
client . updateConnection ( connection ) ;
// 检查远程权限
if ( ! checkRemoteAuthority ( connection ) ) {
// 无权限则抛出无权限的错
throw new RemoteDesignNoAuthException ( ) ;
}
}
private boolean checkRemoteAuthority ( WorkspaceConnection connection ) {
try {
return RemoteAuthorityRepository . getInstance ( ) . isRoot ( connection . getUserName ( ) ) | | RemoteAuthorityRepository . getInstance ( ) . hasAuthority ( connection . getUserName ( ) ) ;
return RemoteNetworkRepository . getInstance ( ) . testConnect ( ) ;
} catch ( RemoteDesignNoAuthException e ) {
throw e ;
} catch ( Exception e ) {
// 如果这里出现异常,就认为fr服务没启动
throw new RemoteDesignConnectionException ( e , StringUtils . EMPTY ) ;
}
}
private LoginRequestInfoBean createLoginBean ( WorkspaceConnectionInfo connectionInfo ) {