use of javax.websocket.CloseReason in project jetty.project by eclipse.
the class AbstractJsrEventDriver method onClose.
@Override
public final void onClose(CloseInfo close) {
if (hasCloseBeenCalled) {
// avoid duplicate close events (possible when using harsh Session.disconnect())
return;
}
hasCloseBeenCalled = true;
CloseCode closecode = CloseCodes.getCloseCode(close.getStatusCode());
CloseReason closereason = new CloseReason(closecode, close.getReason());
onClose(closereason);
}
use of javax.websocket.CloseReason in project groovity by disney.
the class Ws method destroy.
public void destroy() {
openSessions.forEach(session -> {
try {
session.close(new CloseReason(CloseReason.CloseCodes.GOING_AWAY, "Shutting down"));
} catch (IOException e) {
log.log(Level.WARNING, "Error closing client websocket", e);
}
});
openSessions = null;
container = null;
}
use of javax.websocket.CloseReason in project groovity by disney.
the class Ws method tag.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public Object tag(Map attributes, Closure body) throws Exception {
Object url = resolve(attributes, URL);
if (url == null) {
throw new RuntimeException("ws() requires 'url' attribute");
}
ScriptHelper context = getScriptHelper(body);
Map variables = context.getBinding().getVariables();
URI uri;
URIBuilder builder;
ArrayList<Header> headers;
Function handlerFunction;
Optional<UserPass> userPass;
Optional<HttpSignatureSigner> signer;
final AtomicReference openMessage = new AtomicReference<>();
try {
builder = new URIBuilder(url.toString());
bind(context, Uri.CURRENT_URI_BUILDER, builder);
headers = new ArrayList<Header>();
bind(context, com.disney.groovity.tags.Header.CURRENT_LIST_FOR_HEADERS, headers);
Credentials.acceptCredentials(variables);
Signature.acceptSigner(variables);
Object oldOut = get(context, OUT);
StringWriter sw = new StringWriter();
Object rval = null;
bind(context, OUT, sw);
try {
rval = body.call();
if (rval instanceof Writable) {
((Writable) rval).writeTo(sw);
}
} finally {
bind(context, OUT, oldOut);
userPass = Credentials.resolveCredentials(variables);
signer = Signature.resolveSigner(variables);
}
String val = sw.toString().trim();
if (val.length() > 0) {
openMessage.set(val);
} else if (rval != null) {
openMessage.set(rval);
}
uri = builder.build();
handlerFunction = (Function) get(body, Handler.HANDLER_BINDING);
} catch (URISyntaxException e1) {
throw new RuntimeException("Invalid URI " + url, e1);
} finally {
unbind(context, Uri.CURRENT_URI_BUILDER);
unbind(context, com.disney.groovity.tags.Header.CURRENT_LIST_FOR_HEADERS);
unbind(context, Handler.HANDLER_BINDING);
}
final Closure closer = resolve(attributes, CLOSE, Closure.class);
final Closure errorHandler = resolve(attributes, ERROR, Closure.class);
final Class messageFormat = resolve(attributes, MESSAGE, Class.class);
final Integer timeout = resolve(attributes, TIMEOUT, Integer.class);
final AtomicReference<WebSocket> socket = new AtomicReference<>();
ClientEndpointConfig.Builder configBuilder = ClientEndpointConfig.Builder.create();
Session session;
try {
session = getContainer().connectToServer(new Endpoint() {
@Override
public void onOpen(Session session, EndpointConfig config) {
try {
openCount.incrementAndGet();
if (timeout != null) {
session.setMaxIdleTimeout(timeout * 1000);
}
WebSocket ws = new WebSocket(session);
socket.set(ws);
ws.setName(uri.toString());
if (handlerFunction != null) {
ws.setMessageHandler(arg -> {
synchronized (handlerFunction) {
handlerFunction.apply(arg);
}
}, messageFormat);
}
if (openMessage.get() != null) {
ws.call(openMessage.get());
}
} catch (Exception e) {
log.log(Level.SEVERE, "Error opening web socket session " + uri, e);
}
}
@Override
public void onClose(Session session, CloseReason reason) {
try {
closeCount.incrementAndGet();
openSessions.remove(session);
if (closer != null) {
if (closer.getMaximumNumberOfParameters() > 0) {
closer.call(reason);
} else {
closer.call();
}
}
} catch (Exception e) {
log.log(Level.SEVERE, "Error closing web socket session " + uri, e);
}
}
@Override
public void onError(Session session, Throwable th) {
try {
errorCount.incrementAndGet();
if (errorHandler == null) {
throw th;
}
errorHandler.call(th);
} catch (Throwable e) {
Level logLevel = Level.WARNING;
if (th != e) {
log.log(logLevel, "Error handling error for web socket session " + uri, e);
} else if (th instanceof IOException) {
logLevel = Level.FINE;
}
log.log(logLevel, "WebSocket client error: " + uri, th);
}
}
}, configBuilder.configurator(new ClientEndpointConfig.Configurator() {
public void beforeRequest(Map<String, List<String>> reqHeaders) {
// copy programmatic headers
for (Header header : headers) {
List<String> hl = reqHeaders.get(header.getName());
if (hl == null) {
hl = new ArrayList<>();
reqHeaders.put(header.getName(), hl);
}
hl.add(header.getValue());
}
Map<String, Map<String, String>> allChallenges = null;
if (userPass.isPresent() || signer.isPresent()) {
allChallenges = getChallenges(uri, reqHeaders);
}
if (userPass.isPresent()) {
UserPass user = userPass.get();
if (allChallenges != null) {
List<String> auths = reqHeaders.get(AUTHORIZATION_HEADER);
if (auths == null) {
auths = new ArrayList<>();
reqHeaders.put(AUTHORIZATION_HEADER, auths);
}
if (allChallenges.containsKey("basic")) {
StringBuilder authBuilder = new StringBuilder(user.getUser());
authBuilder.append(":");
char[] pass = user.getPass();
for (char c : pass) {
authBuilder.append(c);
}
try {
auths.add("Basic " + printBase64Binary(authBuilder.toString().getBytes("UTF-8")));
} catch (UnsupportedEncodingException e) {
log.severe(e.getMessage());
}
}
if (allChallenges.containsKey("digest")) {
final String digestUri = uri.getPath() + ((uri.getRawQuery() != null) ? "?" + uri.getRawQuery() : "");
Map<String, String> digestChallenge = allChallenges.get("digest");
if (log.isLoggable(Level.FINE)) {
log.fine("Generating digest auth for " + digestChallenge.toString());
}
DigestAuthorization digestAuth = new DigestAuthorization();
digestAuth.setUsername(user.getUser());
digestAuth.setQop("auth");
digestAuth.setCnonce(String.valueOf(ThreadLocalRandom.current().nextLong(10000000, 999999999999l)));
digestAuth.setNonceCount("000001");
digestAuth.setUri(digestUri);
for (Entry<String, String> entry : digestChallenge.entrySet()) {
String k = entry.getKey();
String v = entry.getValue();
if ("nonce".equalsIgnoreCase(k)) {
digestAuth.setNonce(v);
} else if ("realm".equalsIgnoreCase(k)) {
digestAuth.setRealm(v);
} else if ("opaque".equalsIgnoreCase(k)) {
digestAuth.setOpaque(v);
}
}
String signingString;
try {
signingString = digestAuth.generateSigningString(user.getUser(), new String(user.getPass()), new AuthorizationRequest() {
@Override
public String getURI() {
return digestUri;
}
@Override
public String getMethod() {
return "GET";
}
@Override
public List<String> getHeaders(String name) {
return reqHeaders.get(name);
}
});
MessageDigest md5 = MessageDigest.getInstance("MD5");
digestAuth.setDigest(md5.digest(signingString.toString().getBytes()));
if (log.isLoggable(Level.FINE)) {
log.fine("Generated digest auth " + digestAuth.toString());
}
auths.add(digestAuth.toString());
} catch (NoSuchAlgorithmException e) {
log.severe("Missing MD5 " + e.getMessage());
}
}
}
}
if (signer.isPresent()) {
if (allChallenges.containsKey("signature")) {
HttpSignatureSigner sig = signer.get();
HttpGet signReq = createRequest(uri, reqHeaders);
List<Header> beforeHeaders = Arrays.asList(signReq.getAllHeaders());
try {
sig.process(signReq, null);
} catch (HttpException | IOException e) {
log.log(Level.SEVERE, "Error processing http signature", e);
}
Header[] afterHeaders = signReq.getAllHeaders();
for (Header h : afterHeaders) {
if (!beforeHeaders.contains(h)) {
List<String> hl = reqHeaders.get(h.getName());
if (hl == null) {
hl = new ArrayList<>();
reqHeaders.put(h.getName(), hl);
}
hl.add(h.getValue());
if (log.isLoggable(Level.FINE)) {
log.fine("Copied HTTP signature header " + h);
}
}
}
}
}
}
}).build(), uri);
} catch (Exception e) {
errorCount.incrementAndGet();
throw e;
}
openSessions.add(session);
String var = resolve(attributes, VAR, String.class);
if (var != null) {
context.getBinding().setVariable(var, socket.get());
}
return socket.get();
}
use of javax.websocket.CloseReason in project knox by apache.
the class ProxyWebSocketAdapter method getMessageCallback.
private MessageEventCallback getMessageCallback() {
return new MessageEventCallback() {
@Override
public void doCallback(String message) {
/* do nothing */
}
@Override
public void onConnectionOpen(Object session) {
/* do nothing */
}
@Override
public void onConnectionClose(final CloseReason reason) {
try {
frontendSession.close(reason.getCloseCode().getCode(), reason.getReasonPhrase());
} finally {
/* do the cleaning business in seperate thread so we don't block */
pool.execute(new Runnable() {
@Override
public void run() {
closeQuietly();
}
});
}
}
@Override
public void onError(Throwable cause) {
cleanupOnError(cause);
}
@Override
public void onMessageText(String message, Object session) {
final RemoteEndpoint remote = getRemote();
LOG.logMessage("[From Backend <---]" + message);
/* Proxy message to frontend */
try {
remote.sendString(message);
if (remote.getBatchMode() == BatchMode.ON) {
remote.flush();
}
} catch (IOException e) {
LOG.connectionFailed(e);
throw new RuntimeIOException(e);
}
}
@Override
public void onMessageBinary(byte[] message, boolean last, Object session) {
throw new UnsupportedOperationException("Websocket support for binary messages is not supported at this time.");
}
};
}
use of javax.websocket.CloseReason in project muikku by otavanopisto.
the class CoOpsDocumentWebSocket method onMessage.
@OnMessage
public void onMessage(Reader messageReader, Session client, @PathParam("HTMLMATERIALID") String fileId, @PathParam("SESSIONID") String sessionId) throws IOException {
CoOpsSession session = coOpsSessionController.findSessionBySessionId(sessionId);
if (session == null) {
client.close(new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "Not Found"));
return;
}
if (!session.getHtmlMaterial().getId().equals(NumberUtils.createLong(fileId))) {
client.close(new CloseReason(CloseReason.CloseCodes.VIOLATED_POLICY, "Session is associated with another fileId"));
return;
}
ObjectMapper objectMapper = new ObjectMapper();
try {
PatchMessage patchMessage;
try {
patchMessage = objectMapper.readValue(messageReader, PatchMessage.class);
} catch (IOException e) {
throw new CoOpsInternalErrorException(e);
}
if (patchMessage == null) {
throw new CoOpsInternalErrorException("Could not parse message");
}
if (!patchMessage.getType().equals("patch")) {
throw new CoOpsInternalErrorException("Unknown message type: " + patchMessage.getType());
}
Patch patch = patchMessage.getData();
coOpsApi.filePatch(fileId, patch.getSessionId(), patch.getRevisionNumber(), patch.getPatch(), patch.getProperties(), patch.getExtensions());
} catch (CoOpsInternalErrorException e) {
client.close(new CloseReason(CloseReason.CloseCodes.UNEXPECTED_CONDITION, "Internal Error"));
} catch (CoOpsUsageException e) {
client.getAsyncRemote().sendText(objectMapper.writeValueAsString(new ErrorMessage("patchError", 400, e.getMessage())));
} catch (CoOpsNotFoundException e) {
client.getAsyncRemote().sendText(objectMapper.writeValueAsString(new ErrorMessage("patchError", 404, e.getMessage())));
} catch (CoOpsConflictException e) {
client.getAsyncRemote().sendText(objectMapper.writeValueAsString(new ErrorMessage("patchRejected", 409, "Conflict")));
} catch (CoOpsForbiddenException e) {
client.getAsyncRemote().sendText(objectMapper.writeValueAsString(new ErrorMessage("patchError", 500, e.getMessage())));
}
}
Aggregations