kerry
7 years ago
194 changed files with 2792 additions and 3302 deletions
File diff suppressed because one or more lines are too long
@ -0,0 +1,88 @@
|
||||
package com.fr.design.mainframe.loghandler.socketio; |
||||
|
||||
import com.fr.base.env.EnvContext; |
||||
import com.fr.base.env.resource.LocalEnvConfig; |
||||
import com.fr.core.env.EnvConfig; |
||||
import com.fr.core.env.EnvConstants; |
||||
import com.fr.core.env.EnvEvents; |
||||
import com.fr.decision.webservice.utils.DecisionServiceConstants; |
||||
import com.fr.design.mainframe.loghandler.DesignerLogHandler; |
||||
import com.fr.event.Event; |
||||
import com.fr.event.EventDispatcher; |
||||
import com.fr.event.Listener; |
||||
import com.fr.event.Null; |
||||
import com.fr.general.LogRecordTime; |
||||
import com.fr.general.LogUtils; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.third.guava.base.Optional; |
||||
import com.fr.web.WebSocketConfig; |
||||
import io.socket.client.IO; |
||||
import io.socket.client.Socket; |
||||
import io.socket.emitter.Emitter; |
||||
|
||||
import java.io.ByteArrayInputStream; |
||||
import java.net.URI; |
||||
import java.net.URL; |
||||
|
||||
public class DesignerSocketIO { |
||||
|
||||
private static Optional<Socket> socketIO = Optional.absent(); |
||||
|
||||
private static final Emitter.Listener printLog = new Emitter.Listener() { |
||||
@Override |
||||
public void call(Object... objects) { |
||||
try { |
||||
LogRecordTime[] logRecordTimes = LogUtils.readXMLLogRecords(new ByteArrayInputStream((byte[]) objects[0])); |
||||
for (LogRecordTime logRecordTime : logRecordTimes) { |
||||
DesignerLogHandler.getInstance().printRemoteLog(logRecordTime); |
||||
} |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
} |
||||
} |
||||
}; |
||||
|
||||
static { |
||||
EventDispatcher.listen(EnvEvents.AFTER_SIGN_OUT, new Listener<Null>() { |
||||
@Override |
||||
public void on(Event event, Null param) { |
||||
if (socketIO.isPresent()) { |
||||
socketIO.get().close(); |
||||
socketIO = Optional.absent(); |
||||
} |
||||
} |
||||
}); |
||||
EventDispatcher.listen(EnvEvents.AFTER_SIGN_IN, new Listener<Null>() { |
||||
@Override |
||||
public void on(Event event, Null param) { |
||||
updateSocket(); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
public static void init() { |
||||
updateSocket(); |
||||
} |
||||
|
||||
private static void updateSocket() { |
||||
EnvConfig env = EnvContext.currentEnv(); |
||||
if (env instanceof LocalEnvConfig) { |
||||
return; |
||||
} |
||||
|
||||
try { |
||||
String uri = String.format("http://%s:%s%s?%s=%s", |
||||
new URL(env.getPath()).getHost(), |
||||
WebSocketConfig.getInstance().getPort(), |
||||
EnvConstants.WS_NAMESPACE, |
||||
DecisionServiceConstants.WEB_SOCKET_TOKEN_NAME, |
||||
EnvContext.currentToken()); |
||||
|
||||
socketIO = Optional.of(IO.socket(new URI(uri))); |
||||
socketIO.get().on(EnvConstants.WS_LOGRECORD, printLog); |
||||
socketIO.get().connect(); |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
} |
||||
} |
||||
} |
File diff suppressed because it is too large
Load Diff
@ -1,86 +1,167 @@
|
||||
package com.fr.env; |
||||
|
||||
import com.fr.base.EnvException; |
||||
import com.fr.base.FRContext; |
||||
import com.fr.general.IOUtils; |
||||
import com.fr.report.DesignAuthority; |
||||
import com.fr.report.util.AuthorityXMLUtils; |
||||
import com.fr.stable.EncodeConstants; |
||||
import com.fr.third.org.apache.http.HttpEntity; |
||||
import com.fr.third.org.apache.http.client.methods.CloseableHttpResponse; |
||||
import com.fr.third.org.apache.commons.io.IOUtils; |
||||
import com.fr.third.org.apache.http.HttpResponse; |
||||
import com.fr.third.org.apache.http.HttpStatus; |
||||
import com.fr.third.org.apache.http.client.ClientProtocolException; |
||||
import com.fr.third.org.apache.http.client.ResponseHandler; |
||||
import com.fr.third.org.apache.http.client.methods.HttpUriRequest; |
||||
import com.fr.third.org.apache.http.client.methods.RequestBuilder; |
||||
import com.fr.third.org.apache.http.entity.ContentType; |
||||
import com.fr.third.org.apache.http.entity.InputStreamEntity; |
||||
import com.fr.third.org.apache.http.impl.client.CloseableHttpClient; |
||||
import com.fr.third.org.apache.http.impl.client.HttpClients; |
||||
import com.fr.third.org.apache.http.util.EntityUtils; |
||||
import com.fr.web.utils.AuthorityXMLUtils; |
||||
|
||||
import java.io.ByteArrayInputStream; |
||||
import java.io.ByteArrayOutputStream; |
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
import java.net.URLEncoder; |
||||
import java.util.Map; |
||||
|
||||
public class RemoteEnvUtils { |
||||
|
||||
private RemoteEnvUtils() { |
||||
} |
||||
|
||||
public static boolean updateAuthorities(DesignAuthority[] authorities, RemoteEnv env) { |
||||
private static ResponseHandler<InputStream> responseHandler = new ResponseHandler<InputStream>() { |
||||
@Override |
||||
public InputStream handleResponse(HttpResponse response) throws IOException { |
||||
int statusCode = response.getStatusLine().getStatusCode(); |
||||
if (statusCode != HttpStatus.SC_OK) { |
||||
throw new ClientProtocolException("Method failed: " + response.getStatusLine().toString()); |
||||
} |
||||
InputStream in = response.getEntity().getContent(); |
||||
if (in == null) { |
||||
return null; |
||||
} |
||||
// 读取并返回
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
||||
IOUtils.copy(in, out); |
||||
return new ByteArrayInputStream(out.toByteArray()); |
||||
} |
||||
}; |
||||
|
||||
public static InputStream simulateRPCByHttpPost(byte[] bytes, Map<String, String> parameters, boolean isSignIn, RemoteEnv env) throws EnvException { |
||||
String path = env.getPath(); |
||||
String userID = env.getUserID(); |
||||
RequestBuilder builder = RequestBuilder.post(path); |
||||
|
||||
String res = null; |
||||
CloseableHttpClient httpClient = HttpClients.createDefault(); |
||||
InputStream inputStream = null; |
||||
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
||||
for (Map.Entry<String, String> entry : parameters.entrySet()) { |
||||
builder.addParameter(entry.getKey(), entry.getValue()); |
||||
} |
||||
if (!isSignIn) { |
||||
builder.addParameter("id", env.getUserID()); |
||||
} |
||||
InputStreamEntity reqEntity = new InputStreamEntity(new ByteArrayInputStream(bytes)); |
||||
|
||||
AuthorityXMLUtils.writeDesignAuthoritiesXML(authorities, outputStream); |
||||
try (CloseableHttpClient httpClient = HttpClients.createSystem()) { |
||||
HttpUriRequest request = builder |
||||
.setEntity(reqEntity) |
||||
.build(); |
||||
inputStream = httpClient.execute(request, responseHandler); |
||||
} catch (IOException e) { |
||||
FRContext.getLogger().error(e.getMessage()); |
||||
} |
||||
return inputStream; |
||||
} |
||||
|
||||
public static InputStream simulateRPCByHttpPost(Map<String, String> parameters, boolean isSignIn, RemoteEnv env) throws EnvException { |
||||
String path = env.getPath(); |
||||
RequestBuilder builder = RequestBuilder.post(path); |
||||
|
||||
InputStreamEntity reqEntity = new InputStreamEntity(new ByteArrayInputStream(outputStream.toByteArray()), ContentType.TEXT_XML); |
||||
InputStream inputStream = null; |
||||
|
||||
for (Map.Entry<String, String> entry : parameters.entrySet()) { |
||||
builder.addParameter(entry.getKey(), entry.getValue()); |
||||
} |
||||
if (!isSignIn) { |
||||
builder.addParameter("id", env.getUserID()); |
||||
} |
||||
|
||||
HttpUriRequest request = RequestBuilder.post(path) |
||||
.addParameter("id", userID) |
||||
.addParameter("op", "remote_design_authority") |
||||
.addParameter("cmd", "update_authorities") |
||||
.setEntity(reqEntity) |
||||
.build(); |
||||
|
||||
try { |
||||
CloseableHttpResponse response = httpClient.execute(request); |
||||
HttpEntity entity = response.getEntity(); |
||||
res = IOUtils.inputStream2String(entity.getContent(), EncodeConstants.ENCODING_UTF_8); |
||||
EntityUtils.consume(entity); |
||||
try (CloseableHttpClient httpClient = HttpClients.createSystem()) { |
||||
HttpUriRequest request = builder |
||||
.build(); |
||||
inputStream = httpClient.execute(request, responseHandler); |
||||
} catch (IOException e) { |
||||
FRContext.getLogger().error(e.getMessage()); |
||||
} |
||||
return inputStream; |
||||
} |
||||
|
||||
public static InputStream simulateRPCByHttpGet(Map<String, String> parameters, boolean isSignIn, RemoteEnv env) throws EnvException { |
||||
String path = env.getPath(); |
||||
RequestBuilder builder = RequestBuilder.get(path); |
||||
|
||||
return res != null && Boolean.valueOf(res); |
||||
InputStream inputStream = null; |
||||
|
||||
for (Map.Entry<String, String> entry : parameters.entrySet()) { |
||||
builder.addParameter(entry.getKey(), entry.getValue()); |
||||
} |
||||
if (!isSignIn) { |
||||
builder.addParameter("id", env.getUserID()); |
||||
} |
||||
try (CloseableHttpClient httpClient = HttpClients.createSystem()) { |
||||
HttpUriRequest request = builder.build(); |
||||
inputStream = httpClient.execute(request, responseHandler); |
||||
|
||||
} catch (IOException e) { |
||||
FRContext.getLogger().error(e.getMessage()); |
||||
} |
||||
return inputStream; |
||||
} |
||||
|
||||
public static DesignAuthority[] getAuthorities(RemoteEnv env) { |
||||
|
||||
public static InputStream updateAuthorities(DesignAuthority[] authorities, RemoteEnv env) { |
||||
String path = env.getPath(); |
||||
// 远程设计临时用户id
|
||||
String userID = env.getUserID(); |
||||
DesignAuthority[] authorities = null; |
||||
CloseableHttpClient httpClient = HttpClients.createDefault(); |
||||
|
||||
HttpUriRequest request = RequestBuilder.get(path) |
||||
.addParameter("id", userID) |
||||
.addParameter("op", "remote_design_authority") |
||||
.addParameter("cmd", "get_authorities") |
||||
.build(); |
||||
|
||||
try { |
||||
CloseableHttpResponse response = httpClient.execute(request); |
||||
HttpEntity entity = response.getEntity(); |
||||
|
||||
authorities = AuthorityXMLUtils.readDesignAuthoritiesXML(entity.getContent()); |
||||
EntityUtils.consume(entity); |
||||
} catch (Exception e) { |
||||
InputStream inputStream = null; |
||||
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
||||
AuthorityXMLUtils.writeDesignAuthoritiesXML(authorities, outputStream); |
||||
InputStreamEntity reqEntity = new InputStreamEntity(new ByteArrayInputStream(outputStream.toByteArray()), ContentType.TEXT_XML); |
||||
|
||||
try (CloseableHttpClient httpClient = HttpClients.createSystem()) { |
||||
HttpUriRequest request = RequestBuilder.post(path) |
||||
.addParameter("id", userID) |
||||
.addParameter("op", "remote_design_authority") |
||||
.addParameter("cmd", "update_authorities") |
||||
.setEntity(reqEntity) |
||||
.build(); |
||||
inputStream = httpClient.execute(request, responseHandler); |
||||
} catch (IOException e) { |
||||
FRContext.getLogger().error(e.getMessage()); |
||||
} |
||||
return authorities; |
||||
|
||||
return inputStream; |
||||
|
||||
} |
||||
|
||||
public static InputStream getAuthorities(RemoteEnv env) throws EnvException { |
||||
String path = env.getPath(); |
||||
// 远程设计临时用户id
|
||||
String userID = env.getUserID(); |
||||
InputStream inputStream = null; |
||||
|
||||
try (CloseableHttpClient httpClient = HttpClients.createSystem();) { |
||||
HttpUriRequest request = RequestBuilder.get(path) |
||||
.addParameter("id", userID) |
||||
.addParameter("op", "remote_design_authority") |
||||
.addParameter("cmd", "get_authorities") |
||||
.build(); |
||||
inputStream = httpClient.execute(request, responseHandler); |
||||
} catch (IOException e) { |
||||
FRContext.getLogger().error(e.getMessage()); |
||||
} |
||||
return inputStream; |
||||
} |
||||
|
||||
|
||||
} |
||||
|
@ -1,43 +1,61 @@
|
||||
package com.fr.env; |
||||
|
||||
|
||||
import com.fr.base.Env; |
||||
import com.fr.base.FRContext; |
||||
import com.fr.base.env.EnvContext; |
||||
import com.fr.base.env.resource.EnvConfigUtils; |
||||
import com.fr.base.env.resource.RemoteEnvConfig; |
||||
import com.fr.core.env.EnvConfig; |
||||
import com.fr.core.env.EnvEvents; |
||||
import com.fr.dav.LocalEnv; |
||||
import com.fr.design.utils.DesignUtils; |
||||
import com.fr.event.Event; |
||||
import com.fr.event.EventDispatcher; |
||||
import com.fr.event.Listener; |
||||
import com.fr.event.Null; |
||||
import com.fr.general.ComparatorUtils; |
||||
import com.fr.general.env.EnvContext; |
||||
import com.fr.general.Inter; |
||||
|
||||
import javax.swing.JOptionPane; |
||||
import javax.swing.UIManager; |
||||
|
||||
|
||||
public class SignIn { |
||||
public static Env lastSelectedEnv;// 记录最后登录的Env
|
||||
|
||||
static { |
||||
EventDispatcher.listen(EnvEvents.CONNECTION_ERROR, new Listener<Null>() { |
||||
@Override |
||||
public void on(Event event, Null param) { |
||||
if (JOptionPane.showConfirmDialog(null, Inter.getLocText("FR-Remote_Connect2Server_Again"), UIManager.getString("OptionPane.titleText"), JOptionPane.YES_NO_OPTION) |
||||
== JOptionPane.OK_OPTION) { |
||||
try { |
||||
EnvContext.signIn(EnvContext.currentEnv()); |
||||
} catch (Exception e) { |
||||
FRContext.getLogger().error(e.getMessage()); |
||||
} |
||||
} |
||||
} |
||||
}); |
||||
} |
||||
|
||||
/** |
||||
* 注册入环境 |
||||
* @param selectedEnv 选择的环境 |
||||
* @throws Exception 异常 |
||||
*/ |
||||
public static void signIn(Env selectedEnv) throws Exception { |
||||
boolean validServer; |
||||
signOutOldEnv(selectedEnv); |
||||
selectedEnv.signIn(); |
||||
validServer = true; |
||||
if (validServer) { |
||||
DesignUtils.switchToEnv(selectedEnv); |
||||
lastSelectedEnv = selectedEnv; |
||||
public static void signIn(EnvConfig selectedEnv) throws Exception { |
||||
if (EnvContext.currentEnv() != null && !ComparatorUtils.equals(EnvContext.currentEnv(), selectedEnv)) { |
||||
EnvContext.signOut(); |
||||
} |
||||
EnvContext.signIn(selectedEnv); |
||||
DesignUtils.switchToEnv(trans(selectedEnv)); |
||||
} |
||||
|
||||
private static void signOutOldEnv(Env newEnv) { |
||||
// 环境相同直接返回,避免浪费过多时间
|
||||
if (lastSelectedEnv == null || ComparatorUtils.equals(lastSelectedEnv, newEnv)) { |
||||
return; |
||||
} |
||||
try { |
||||
EnvContext.fireBeforeSignOut(); |
||||
lastSelectedEnv.signOut(); |
||||
EnvContext.fireAfterSignOut(); |
||||
} catch (Exception e) { |
||||
FRContext.getLogger().error(e.getMessage(), e); |
||||
private static com.fr.base.Env trans(EnvConfig env) { |
||||
if (env instanceof RemoteEnvConfig) { |
||||
return new RemoteEnv(env.getPath(), EnvConfigUtils.getUsername(env), EnvConfigUtils.getPassword(env)); |
||||
} else { |
||||
return new LocalEnv(); |
||||
} |
||||
} |
||||
} |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue