index
int64
repo_id
string
file_path
string
content
string
0
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm/js/JsGuestModuleObject.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.vm.js; import java.util.Iterator; import org.graalvm.polyglot.Value; import org.graalvm.polyglot.proxy.ProxyObject; import swim.collections.HashTrieMap; import swim.vm.VmProxyArray; public class JsGuestModuleObject implements ProxyObject { final JsGuestModule module; HashTrieMap<String, Value> dynamicMembers; public JsGuestModuleObject(JsGuestModule module) { this.module = module; this.dynamicMembers = HashTrieMap.empty(); } public final JsGuestModule module() { return this.module; } @Override public boolean hasMember(String key) { if ("id".equals(key)) { return true; } else if ("exports".equals(key)) { return true; } else { return hasDynamicMember(key); } } protected boolean hasDynamicMember(String key) { return this.dynamicMembers.containsKey(key); } @Override public Object getMember(String key) { if ("id".equals(key)) { return this.module.moduleId.toString(); } else if ("exports".equals(key)) { return this.module.moduleExports; } else { return getDynamicMember(key); } } protected Object getDynamicMember(String key) { return this.dynamicMembers.get(key); } @Override public void putMember(String key, Value value) { if ("id".equals(key)) { throw new UnsupportedOperationException(); } else if ("exports".equals(key)) { this.module.setModuleExports(value); } else { putDynamicMember(key, value); } } protected void putDynamicMember(String key, Value value) { this.dynamicMembers = this.dynamicMembers.updated(key, value); } @Override public boolean removeMember(String key) { if ("id".equals(key)) { throw new UnsupportedOperationException(); } else if ("exports".equals(key)) { throw new UnsupportedOperationException(); } else { return removeDynamicMember(key); } } protected boolean removeDynamicMember(String key) { final HashTrieMap<String, Value> oldDynamicMembers = this.dynamicMembers; final HashTrieMap<String, Value> newDynamicMembers = oldDynamicMembers.removed(key); if (oldDynamicMembers != newDynamicMembers) { this.dynamicMembers = newDynamicMembers; return true; } else { return false; } } @Override public Object getMemberKeys() { final HashTrieMap<String, Value> dynamicMembers = this.dynamicMembers; final String[] memberKeys = new String[2 + dynamicMembers.size()]; memberKeys[0] = "id"; memberKeys[1] = "exports"; final Iterator<String> dynamicMemberKeyIterator = dynamicMembers.keyIterator(); int i = 2; while (dynamicMemberKeyIterator.hasNext()) { memberKeys[i] = dynamicMemberKeyIterator.next(); i += 1; } return new VmProxyArray(memberKeys); } }
0
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm/js/JsHostClass.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.vm.js; import java.util.Collection; import org.graalvm.polyglot.Value; import org.graalvm.polyglot.proxy.ProxyInstantiable; import org.graalvm.polyglot.proxy.ProxyObject; import swim.dynamic.HostClassType; import swim.dynamic.HostConstructor; import swim.dynamic.HostStaticField; import swim.dynamic.HostStaticMember; import swim.dynamic.HostStaticMethod; import swim.vm.VmProxyArray; public class JsHostClass implements ProxyObject, ProxyInstantiable { final JsBridge bridge; final HostClassType<?> type; public JsHostClass(JsBridge bridge, HostClassType<?> type) { this.bridge = bridge; this.type = type; } @Override public boolean hasMember(String key) { if ("prototype".equals(key)) { return true; } else if ("__proto__".equals(key)) { return true; } else { final HostStaticMember staticMember = this.type.getStaticMember(this.bridge, key); return staticMember != null; } } @Override public Object getMember(String key) { if ("prototype".equals(key)) { return this.bridge.hostTypeToGuestPrototype(this.type); } else if ("__proto__".equals(key)) { return this.bridge.guestFunctionPrototype(); } else { final HostStaticMember staticMember = this.type.getStaticMember(this.bridge, key); if (staticMember instanceof HostStaticField) { final Object hostValue = ((HostStaticField) staticMember).get(this.bridge); return this.bridge.hostToGuest(hostValue); } else if (staticMember instanceof HostStaticMethod) { return this.bridge.hostStaticMethodToGuestStaticMethod((HostStaticMethod) staticMember); } else { return null; } } } @Override public void putMember(String key, Value guestValue) { System.out.println("HsHostClass.putMember key: " + key + "; guestValue: " + guestValue); if ("prototype".equals(key)) { throw new UnsupportedOperationException(); } else if ("__proto__".equals(key)) { throw new UnsupportedOperationException(); } else { final HostStaticMember staticMember = this.type.getStaticMember(this.bridge, key); if (staticMember instanceof HostStaticField) { final Object hostValue = this.bridge.guestToHost(guestValue); ((HostStaticField) staticMember).set(this.bridge, hostValue); } else { throw new UnsupportedOperationException(); } } } @Override public boolean removeMember(String key) { if ("prototype".equals(key)) { throw new UnsupportedOperationException(); } else if ("__proto__".equals(key)) { throw new UnsupportedOperationException(); } else { final HostStaticMember staticMember = this.type.getStaticMember(this.bridge, key); if (staticMember instanceof HostStaticField) { return ((HostStaticField) staticMember).remove(this.bridge); } else { throw new UnsupportedOperationException(); } } } @Override public Object getMemberKeys() { final Collection<? extends HostStaticMember> staticMembers = this.type.staticMembers(this.bridge); final String[] staticMemberKeys = new String[staticMembers.size() + 2]; int i = 0; for (HostStaticMember staticMember : staticMembers) { staticMemberKeys[i] = staticMember.key(); i += 1; } staticMemberKeys[i] = "constructor"; staticMemberKeys[i + 1] = "__proto__"; return new VmProxyArray(staticMemberKeys); } @Override public Object newInstance(Value... guestArguments) { final HostConstructor constructor = this.type.constructor(this.bridge); if (constructor != null) { final int arity = guestArguments.length; final Object[] hostArguments = new Object[arity]; for (int i = 0; i < arity; i += 1) { hostArguments[i] = this.bridge.guestToHost(guestArguments[i]); } final Object hostInstance = constructor.newInstance(this.bridge, hostArguments); final Object guestInstance = this.bridge.hostToGuest(hostInstance); return guestInstance; } else { throw new UnsupportedOperationException(); } } @Override public String toString() { return '[' + "JsHostClass " + this.type.hostClass().getName() + ']'; } }
0
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm/js/JsHostLibraryModule.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.vm.js; import java.util.Collection; import org.graalvm.polyglot.Value; import org.graalvm.polyglot.proxy.ProxyObject; import swim.dynamic.HostLibrary; import swim.dynamic.HostType; import swim.uri.UriPath; import swim.vm.VmProxyArray; public class JsHostLibraryModule implements JsModule, ProxyObject { final JsBridge bridge; final JsModuleSystem moduleSystem; final UriPath moduleId; final HostLibrary library; public JsHostLibraryModule(JsBridge bridge, JsModuleSystem moduleSystem, UriPath moduleId, HostLibrary library) { this.bridge = bridge; this.moduleSystem = moduleSystem; this.moduleId = moduleId; this.library = library; } @Override public final JsModuleSystem moduleSystem() { return this.moduleSystem; } @Override public final UriPath moduleId() { return this.moduleId; } @Override public Value moduleExports() { return this.bridge.jsContext.asValue(this); } @Override public void evalModule() { // nop } @Override public boolean hasMember(String key) { return this.library.getHostType(key) != null; } @Override public Object getMember(String key) { final HostType<?> typeMember = this.library.getHostType(key); return typeMember != null ? this.bridge.hostTypeToGuestType(typeMember) : null; } @Override public void putMember(String key, Value guestValue) { throw new UnsupportedOperationException(); } @Override public boolean removeMember(String key) { throw new UnsupportedOperationException(); } @Override public Object getMemberKeys() { final Collection<HostType<?>> typeMembers = this.library.hostTypes(); final String[] typeMemberKeys = new String[typeMembers.size()]; int i = 0; for (HostType<?> typeMember : typeMembers) { typeMemberKeys[i] = typeMember.typeName(); i += 1; } return new VmProxyArray(typeMemberKeys); } }
0
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm/js/JsHostMethod.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.vm.js; import org.graalvm.polyglot.Value; import org.graalvm.polyglot.proxy.ProxyExecutable; import org.graalvm.polyglot.proxy.ProxyObject; import swim.dynamic.HostMethod; import swim.vm.VmProxyArray; public class JsHostMethod<T> implements ProxyExecutable, ProxyObject { final JsBridge bridge; final HostMethod<? super T> method; final T self; public JsHostMethod(JsBridge bridge, HostMethod<? super T> method, T self) { this.bridge = bridge; this.method = method; this.self = self; } @Override public Object execute(Value... guestArguments) { final int arity = guestArguments.length; final Object[] hostArguments = new Object[arity]; for (int i = 0; i < arity; i += 1) { hostArguments[i] = this.bridge.guestToHost(guestArguments[i]); } final Object hostResult = this.method.invoke(this.bridge, this.self, hostArguments); final Object guestResult = this.bridge.hostToGuest(hostResult); return guestResult; } @Override public boolean hasMember(String key) { if ("__proto__".equals(key)) { return true; } else { return false; } } @Override public Object getMember(String key) { if ("__proto__".equals(key)) { return this.bridge.guestFunctionPrototype(); } else { return null; } } @Override public void putMember(String key, Value guestValue) { throw new UnsupportedOperationException(); } @Override public boolean removeMember(String key) { throw new UnsupportedOperationException(); } @Override public Object getMemberKeys() { return new VmProxyArray(new String[] {"__proto__"}); } }
0
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm/js/JsHostObject.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.vm.js; import java.util.Collection; import org.graalvm.polyglot.Value; import org.graalvm.polyglot.proxy.ProxyObject; import swim.dynamic.HostField; import swim.dynamic.HostMember; import swim.dynamic.HostMethod; import swim.dynamic.HostObjectType; import swim.vm.VmHostProxy; import swim.vm.VmProxyArray; public class JsHostObject<T> extends VmHostProxy<T> implements ProxyObject { final JsBridge bridge; final HostObjectType<? super T> type; final T self; public JsHostObject(JsBridge bridge, HostObjectType<? super T> type, T self) { this.bridge = bridge; this.type = type; this.self = self; } @Override public final T unwrap() { return this.self; } @Override public boolean hasMember(String key) { if ("__proto__".equals(key)) { return true; } else { final HostMember<? super T> member = this.type.getMember(this.bridge, this.self, key); return member != null; } } @Override public Object getMember(String key) { if ("__proto__".equals(key)) { return this.bridge.hostTypeToGuestPrototype(this.type); } else { final HostMember<? super T> member = this.type.getMember(this.bridge, this.self, key); if (member instanceof HostField<?>) { final Object hostValue = ((HostField<? super T>) member).get(this.bridge, this.self); return this.bridge.hostToGuest(hostValue); } else if (member instanceof HostMethod<?>) { return this.bridge.hostMethodToGuestMethod((HostMethod<? super T>) member, this.self); } else { return null; } } } @Override public void putMember(String key, Value guestValue) { if ("__proto__".equals(key)) { throw new UnsupportedOperationException(); } else { final HostMember<? super T> member = this.type.getMember(this.bridge, this.self, key); if (member instanceof HostField<?>) { final Object hostValue = this.bridge.guestToHost(guestValue); ((HostField<? super T>) member).set(this.bridge, this.self, hostValue); } else { throw new UnsupportedOperationException(); } } } @Override public boolean removeMember(String key) { if ("__proto__".equals(key)) { throw new UnsupportedOperationException(); } else { final HostMember<? super T> member = this.type.getMember(this.bridge, this.self, key); if (member instanceof HostField<?>) { return ((HostField<? super T>) member).remove(this.bridge, this.self); } else { throw new UnsupportedOperationException(); } } } @Override public Object getMemberKeys() { final Collection<? extends HostMember<? super T>> members = this.type.members(this.bridge, this.self); final String[] memberKeys = new String[members.size() + 1]; int i = 0; for (HostMember<? super T> member : members) { memberKeys[i] = member.key(); i += 1; } memberKeys[i] = "__proto__"; return new VmProxyArray(memberKeys); } }
0
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm/js/JsHostPrototype.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.vm.js; import java.util.Collection; import org.graalvm.polyglot.Value; import org.graalvm.polyglot.proxy.ProxyObject; import swim.dynamic.HostClassType; import swim.dynamic.HostField; import swim.dynamic.HostMember; import swim.dynamic.HostMethod; import swim.vm.VmProxyArray; public class JsHostPrototype implements ProxyObject { final JsBridge bridge; final HostClassType<?> type; public JsHostPrototype(JsBridge bridge, HostClassType<?> type) { this.bridge = bridge; this.type = type; } @Override public boolean hasMember(String key) { if ("constructor".equals(key)) { return true; } else if ("__proto__".equals(key)) { return true; } else { final HostMember<?> member = this.type.getMember(this.bridge, null, key); return member != null; } } @SuppressWarnings("unchecked") @Override public Object getMember(String key) { if ("constructor".equals(key)) { return this.bridge.hostTypeToGuestType(this.type); } else if ("__proto__".equals(key)) { return this.bridge.hostTypeToGuestPrototype(this.type.superType()); } else { final HostMember<?> member = this.type.getMember(this.bridge, null, key); if (member instanceof HostField<?>) { final Object hostValue = ((HostField<?>) member).get(this.bridge, null); return this.bridge.hostToGuest(hostValue); } else if (member instanceof HostMethod<?>) { return this.bridge.hostMethodToGuestMethod((HostMethod<Object>) member, null); } else { return null; } } } @Override public void putMember(String key, Value guestValue) { if ("constructor".equals(key)) { throw new UnsupportedOperationException(); } else if ("__proto__".equals(key)) { throw new UnsupportedOperationException(); } else { final HostMember<?> member = this.type.getMember(this.bridge, null, key); if (member instanceof HostField<?>) { final Object hostValue = this.bridge.guestToHost(guestValue); ((HostField<?>) member).set(this.bridge, null, hostValue); } else { throw new UnsupportedOperationException(); } } } @Override public boolean removeMember(String key) { if ("constructor".equals(key)) { throw new UnsupportedOperationException(); } else if ("__proto__".equals(key)) { throw new UnsupportedOperationException(); } else { final HostMember<?> member = this.type.getMember(this.bridge, null, key); if (member instanceof HostField<?>) { return ((HostField<?>) member).remove(this.bridge, null); } else { throw new UnsupportedOperationException(); } } } @Override public Object getMemberKeys() { final Collection<? extends HostMember<?>> members = this.type.members(this.bridge, null); final String[] memberKeys = new String[members.size() + 2]; int i = 0; for (HostMember<?> member : members) { memberKeys[i] = member.key(); i += 1; } memberKeys[i] = "constructor"; memberKeys[i + 1] = "__proto__"; return new VmProxyArray(memberKeys); } @Override public String toString() { return '[' + "JsHostPrototype " + this.type.hostClass().getName() + ']'; } }
0
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm/js/JsHostRuntime.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.vm.js; import java.util.Map; import swim.collections.HashTrieMap; import swim.dynamic.HostLibrary; import swim.dynamic.JavaHostRuntime; import swim.uri.UriPath; public class JsHostRuntime extends JavaHostRuntime implements JsRuntime { JsModuleResolver moduleResolver; HashTrieMap<UriPath, HostLibrary> hostModules; public JsHostRuntime(JsModuleResolver moduleResolver) { this.moduleResolver = moduleResolver; this.hostModules = HashTrieMap.empty(); } public JsHostRuntime() { this(new JsNodeModuleResolver()); } @Override public final JsModuleResolver moduleResolver() { return this.moduleResolver; } @Override public void setModuleResolver(JsModuleResolver moduleResolver) { this.moduleResolver = moduleResolver; } @Override public final HostLibrary getHostModule(UriPath moduleId) { return this.hostModules.get(moduleId); } @Override public final Map<UriPath, HostLibrary> hostModules() { return this.hostModules; } public void addHostModule(UriPath moduleId, HostLibrary hostLibrary) { this.hostModules = this.hostModules.updated(moduleId, hostLibrary); } public void addHostModule(String moduleId, HostLibrary hostLibrary) { addHostModule(UriPath.parse(moduleId), hostLibrary); } }
0
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm/js/JsHostStaticMethod.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.vm.js; import org.graalvm.polyglot.Value; import org.graalvm.polyglot.proxy.ProxyExecutable; import org.graalvm.polyglot.proxy.ProxyObject; import swim.dynamic.HostStaticMethod; import swim.vm.VmProxyArray; public class JsHostStaticMethod implements ProxyExecutable, ProxyObject { final JsBridge bridge; final HostStaticMethod staticMethod; public JsHostStaticMethod(JsBridge bridge, HostStaticMethod staticMethod) { this.bridge = bridge; this.staticMethod = staticMethod; } @Override public Object execute(Value... guestArguments) { final int arity = guestArguments.length; final Object[] hostArguments = new Object[arity]; for (int i = 0; i < arity; i += 1) { hostArguments[i] = this.bridge.guestToHost(guestArguments[i]); } final Object hostResult = this.staticMethod.invoke(this.bridge, hostArguments); final Object guestResult = this.bridge.hostToGuest(hostResult); return guestResult; } @Override public boolean hasMember(String key) { if ("__proto__".equals(key)) { return true; } else { return false; } } @Override public Object getMember(String key) { if ("__proto__".equals(key)) { return this.bridge.guestFunctionPrototype(); } else { return null; } } @Override public void putMember(String key, Value guestValue) { throw new UnsupportedOperationException(); } @Override public boolean removeMember(String key) { throw new UnsupportedOperationException(); } @Override public Object getMemberKeys() { return new VmProxyArray(new String[] {"__proto__"}); } }
0
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm/js/JsHostType.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.vm.js; import java.util.Collection; import org.graalvm.polyglot.Value; import org.graalvm.polyglot.proxy.ProxyObject; import swim.dynamic.HostStaticField; import swim.dynamic.HostStaticMember; import swim.dynamic.HostStaticMethod; import swim.dynamic.HostType; import swim.vm.VmProxyArray; public class JsHostType implements ProxyObject { final JsBridge bridge; final HostType<?> type; public JsHostType(JsBridge bridge, HostType<?> type) { this.bridge = bridge; this.type = type; } @Override public boolean hasMember(String key) { if ("__proto__".equals(key)) { return true; } else { final HostStaticMember staticMember = this.type.getStaticMember(this.bridge, key); return staticMember != null; } } @Override public Object getMember(String key) { if ("__proto__".equals(key)) { return this.bridge.guestObjectPrototype(); } else { final HostStaticMember staticMember = this.type.getStaticMember(this.bridge, key); if (staticMember instanceof HostStaticField) { final Object hostValue = ((HostStaticField) staticMember).get(this.bridge); return this.bridge.hostToGuest(hostValue); } else if (staticMember instanceof HostStaticMethod) { return this.bridge.hostStaticMethodToGuestStaticMethod((HostStaticMethod) staticMember); } else { return null; } } } @Override public void putMember(String key, Value guestValue) { if ("__proto__".equals(key)) { throw new UnsupportedOperationException(); } else { final HostStaticMember staticMember = this.type.getStaticMember(this.bridge, key); if (staticMember instanceof HostStaticField) { final Object hostValue = this.bridge.guestToHost(guestValue); ((HostStaticField) staticMember).set(this.bridge, hostValue); } else { throw new UnsupportedOperationException(); } } } @Override public boolean removeMember(String key) { if ("__proto__".equals(key)) { throw new UnsupportedOperationException(); } else { final HostStaticMember staticMember = this.type.getStaticMember(this.bridge, key); if (staticMember instanceof HostStaticField) { return ((HostStaticField) staticMember).remove(this.bridge); } else { throw new UnsupportedOperationException(); } } } @Override public Object getMemberKeys() { final Collection<? extends HostStaticMember> staticMembers = this.type.staticMembers(this.bridge); final String[] staticMemberKeys = new String[staticMembers.size() + 1]; int i = 0; for (HostStaticMember staticMember : staticMembers) { staticMemberKeys[i] = staticMember.key(); i += 1; } staticMemberKeys[i] = "__proto__"; return new VmProxyArray(staticMemberKeys); } @Override public String toString() { return '[' + "JsHostType " + this.type.hostClass().getName() + ']'; } }
0
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm/js/JsModule.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.vm.js; import org.graalvm.polyglot.Value; import swim.uri.UriPath; public interface JsModule { JsModuleSystem moduleSystem(); UriPath moduleId(); Value moduleExports(); void evalModule(); }
0
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm/js/JsModuleException.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.vm.js; public class JsModuleException extends RuntimeException { private static final long serialVersionUID = 1L; public JsModuleException(String message, Throwable cause) { super(message, cause); } public JsModuleException(String message) { super(message); } public JsModuleException(Throwable cause) { super(cause); } public JsModuleException() { super(); } }
0
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm/js/JsModuleLoader.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.vm.js; import swim.uri.UriPath; public interface JsModuleLoader { UriPath resolveModulePath(UriPath basePath, UriPath modulePath); JsModule loadModule(JsModuleSystem moduleSystem, UriPath moduleId); void evalModule(JsModule module); }
0
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm/js/JsModuleResolver.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.vm.js; import org.graalvm.polyglot.Source; import swim.uri.UriPath; public interface JsModuleResolver { UriPath resolveModulePath(UriPath basePath, UriPath modulePath); Source loadModuleSource(UriPath moduleId); }
0
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm/js/JsModuleSystem.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.vm.js; import org.graalvm.polyglot.Context; import swim.codec.Format; import swim.collections.HashTrieMap; import swim.uri.UriPath; public class JsModuleSystem { final Context jsContext; final JsModuleLoader moduleLoader; HashTrieMap<UriPath, JsModule> modules; public JsModuleSystem(Context jsContext, JsModuleLoader moduleLoader) { this.jsContext = jsContext; this.moduleLoader = moduleLoader; this.modules = HashTrieMap.empty(); } public JsModuleSystem(Context jsContext) { this(jsContext, new JsGuestModuleLoader()); } public final Context jsContext() { return this.jsContext; } public final JsModuleLoader moduleLoader() { return this.moduleLoader; } public UriPath resolveModulePath(UriPath basePath, UriPath modulePath) { return this.moduleLoader.resolveModulePath(basePath, modulePath); } public final JsModule getModule(UriPath moduleId) { return this.modules.get(moduleId); } public final JsModule requireModule(UriPath basePath, UriPath modulePath) { final UriPath moduleId = resolveModulePath(basePath, modulePath); if (moduleId != null) { return requireModule(moduleId); } else { throw new JsModuleException("failed to resolve module " + Format.debug(modulePath.toString()) + " relative to " + Format.debug(basePath.toString())); } } public final JsModule requireModule(UriPath moduleId) { JsModule module = getModule(moduleId); if (module == null) { module = openModule(moduleId); } return module; } protected final JsModule openModule(UriPath moduleId) { final JsModule module = loadModule(moduleId); if (module != null) { this.modules = this.modules.updated(moduleId, module); evalModule(module); return module; } else { throw new JsModuleException("failed to load module " + Format.debug(moduleId.toString())); } } protected JsModule loadModule(UriPath moduleId) { return this.moduleLoader.loadModule(this, moduleId); } protected void evalModule(JsModule module) { this.moduleLoader.evalModule(module); } public static boolean isRelativeModulePath(UriPath modulePath) { if (modulePath.isDefined() && modulePath.isRelative()) { final String head = modulePath.head(); return ".".equals(head) || "..".equals(head); } return false; } }
0
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm/js/JsNodeModuleResolver.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.vm.js; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.graalvm.polyglot.Source; import swim.codec.Decoder; import swim.codec.Unicode; import swim.codec.Utf8; import swim.json.Json; import swim.structure.Value; import swim.uri.UriPath; public class JsNodeModuleResolver implements JsModuleResolver { final UriPath rootPath; public JsNodeModuleResolver(UriPath rootPath) { this.rootPath = rootPath; } public JsNodeModuleResolver() { this(UriPath.empty()); } public final UriPath rootPath() { return this.rootPath; } @Override public UriPath resolveModulePath(UriPath basePath, UriPath modulePath) { if (JsModuleSystem.isRelativeModulePath(modulePath)) { return resolveRelativeModulePath(basePath, modulePath); } else { return resolveAbsoluteModulePath(basePath, modulePath); } } protected UriPath resolveRelativeModulePath(UriPath basePath, UriPath modulePath) { final UriPath absolutePath = basePath.appended(modulePath).removeDotSegments(); UriPath moduleId = resolveModuleScript(absolutePath, "js"); if (moduleId == null) { moduleId = resolveModuleDirectory(absolutePath); } return moduleId; } protected UriPath resolveAbsoluteModulePath(UriPath basePath, UriPath modulePath) { UriPath directoryPath = basePath.base(); UriPath moduleId; do { moduleId = resolveNodeModulesPath(directoryPath, modulePath); if (moduleId != null) { break; } directoryPath = directoryPath.parent(); } while (directoryPath.isDefined() && directoryPath.isSubpathOf(this.rootPath)); return moduleId; } public UriPath resolveNodeModulesPath(UriPath directoryPath, UriPath modulePath) { final UriPath nodeModulesDirectoryPath = directoryPath.appendedSegment("node_modules"); final File nodeModulesDirectory = new File(nodeModulesDirectoryPath.toString()); if (nodeModulesDirectory.isDirectory()) { return resolveRelativeModulePath(nodeModulesDirectoryPath, modulePath); } return null; } public UriPath resolveModuleScript(UriPath modulePath, String extension) { final UriPath moduleFoot = modulePath.foot(); if (moduleFoot.isDefined() && moduleFoot.isRelative()) { final String scriptName = moduleFoot.head() + '.' + extension; final UriPath scriptPath = modulePath.name(scriptName); final File scriptFile = new File(scriptPath.toString()); if (scriptFile.exists()) { return scriptPath; } } return null; } public UriPath resolveModuleDirectory(UriPath directoryPath) { final File directory = new File(directoryPath.toString()); if (directory.isDirectory()) { UriPath modulePath = resolveModuleDirectoryPackage(directoryPath); if (modulePath == null) { modulePath = resolveModuleDirectoryIndex(directoryPath, "js"); } return modulePath; } return null; } public UriPath resolveModuleDirectoryIndex(UriPath directoryPath, String extension) { final String scriptName = "index" + '.' + extension; final UriPath scriptPath = directoryPath.appendedSegment(scriptName); final File scriptFile = new File(scriptPath.toString()); if (scriptFile.exists()) { return scriptPath; } return null; } public UriPath resolveModuleDirectoryPackage(UriPath directoryPath) { final UriPath packagePath = directoryPath.appendedSegment("package.json"); final File packageFile = new File(packagePath.toString()); if (packageFile.exists()) { return resolveModulePackage(packagePath); } return null; } public UriPath resolveModulePackage(UriPath packagePath) { // TODO: cache package values final Value packageValue = loadPackage(packagePath); final String main = packageValue.get("main").stringValue(null); if (main != null) { final UriPath scriptPath = packagePath.resolve(UriPath.parse(main)); if (scriptPath.isSubpathOf(this.rootPath)) { final File scriptFile = new File(scriptPath.toString()); if (scriptFile.exists()) { return scriptPath; } } } return null; } public Value loadPackage(UriPath packagePath) { FileInputStream packageInput = null; try { packageInput = new FileInputStream(packagePath.toString()); return Utf8.read(Json.structureParser().objectParser(), packageInput); } catch (IOException cause) { throw new JsModuleException(cause); } finally { try { if (packageInput != null) { packageInput.close(); } } catch (IOException swallow) { } } } protected void prefixModuleSource(UriPath moduleId, StringBuilder sourceBuilder) { sourceBuilder.append("(function(require,module,exports){"); } protected void suffixModuleSource(UriPath moduleId, StringBuilder sourceBuilder) { sourceBuilder.append("})(require,module,exports);"); } @Override public Source loadModuleSource(UriPath moduleId) { final String moduleName = moduleId.toString(); FileInputStream sourceInput = null; try { sourceInput = new FileInputStream(moduleName.toString()); final StringBuilder sourceBuilder = new StringBuilder(); prefixModuleSource(moduleId, sourceBuilder); final Decoder<String> sourceDecoder = Utf8.decode(Unicode.stringParser(sourceBuilder), sourceInput); if (sourceDecoder.isDone()) { suffixModuleSource(moduleId, sourceBuilder); final String source = sourceBuilder.toString(); return Source.newBuilder("js", source, moduleName).buildLiteral(); } else { throw new JsModuleException(sourceDecoder.trap()); } } catch (IOException cause) { throw new JsModuleException(cause); } finally { try { if (sourceInput != null) { sourceInput.close(); } } catch (IOException swallow) { } } } }
0
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm/js/JsRequireFunction.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.vm.js; import java.util.Iterator; import org.graalvm.polyglot.Value; import org.graalvm.polyglot.proxy.ProxyExecutable; import org.graalvm.polyglot.proxy.ProxyObject; import swim.collections.HashTrieMap; import swim.uri.UriPath; import swim.vm.VmProxyArray; public class JsRequireFunction implements ProxyExecutable, ProxyObject { final JsGuestModule module; HashTrieMap<String, Value> members; public JsRequireFunction(JsGuestModule module) { this.module = module; this.members = HashTrieMap.empty(); } public final JsGuestModule module() { return this.module; } @Override public Object execute(Value... arguments) { final UriPath modulePath = UriPath.parse(arguments[0].asString()); final JsModule module = this.module.requireModule(modulePath); return module.moduleExports(); } @Override public boolean hasMember(String key) { return this.members.containsKey(key); } @Override public Object getMember(String key) { return this.members.get(key); } @Override public void putMember(String key, Value value) { this.members = this.members.updated(key, value); } @Override public boolean removeMember(String key) { final HashTrieMap<String, Value> oldMembers = this.members; final HashTrieMap<String, Value> newMembers = oldMembers.removed(key); if (oldMembers != newMembers) { this.members = newMembers; return true; } else { return false; } } @Override public Object getMemberKeys() { final HashTrieMap<String, Value> members = this.members; final String[] memberKeys = new String[members.size()]; final Iterator<String> memberKeyIterator = members.keyIterator(); int i = 0; while (memberKeyIterator.hasNext()) { memberKeys[i] = memberKeyIterator.next(); i += 1; } return new VmProxyArray(memberKeys); } }
0
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm/js/JsRuntime.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.vm.js; import java.util.Map; import swim.dynamic.HostLibrary; import swim.dynamic.HostRuntime; import swim.uri.UriPath; public interface JsRuntime extends HostRuntime { JsModuleResolver moduleResolver(); void setModuleResolver(JsModuleResolver moduleResolver); HostLibrary getHostModule(UriPath moduleId); Map<UriPath, HostLibrary> hostModules(); }
0
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm/js/JsStaticModuleResolver.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.vm.js; import org.graalvm.polyglot.Source; import swim.collections.HashTrieMap; import swim.uri.UriPath; public class JsStaticModuleResolver implements JsModuleResolver { HashTrieMap<UriPath, Source> moduleSources; public JsStaticModuleResolver() { this.moduleSources = HashTrieMap.empty(); } @Override public UriPath resolveModulePath(UriPath basePath, UriPath modulePath) { return basePath.resolve(modulePath); } @Override public Source loadModuleSource(UriPath moduleId) { return this.moduleSources.get(moduleId); } protected void prefixModuleSource(UriPath moduleId, StringBuilder sourceBuilder) { sourceBuilder.append("(function(require,module,exports){"); } protected void suffixModuleSource(UriPath moduleId, StringBuilder sourceBuilder) { sourceBuilder.append("})(require,module,exports);"); } protected String processModuleSource(UriPath moduleId, String source) { final StringBuilder sourceBuilder = new StringBuilder(); prefixModuleSource(moduleId, sourceBuilder); sourceBuilder.append(source); suffixModuleSource(moduleId, sourceBuilder); return sourceBuilder.toString(); } public void defineModuleSource(UriPath moduleId, String source) { source = processModuleSource(moduleId, source); final String moduleName = moduleId.toString(); final Source moduleSource = Source.newBuilder("js", source, moduleName).buildLiteral(); this.moduleSources = this.moduleSources.updated(moduleId, moduleSource); } public void defineModuleSource(String moduleId, String source) { defineModuleSource(UriPath.parse(moduleId), source); } }
0
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm
java-sources/ai/swim/swim-vm-js/3.10.0/swim/vm/js/package-info.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /** * Swim JavaScript language integration. */ package swim.vm.js;
0
java-sources/ai/swim/swim-warp
java-sources/ai/swim/swim-warp/3.10.0/module-info.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /** * Web Agent Remote Protocol. */ module swim.warp { requires swim.util; requires transitive swim.codec; requires transitive swim.uri; requires transitive swim.structure; requires swim.recon; exports swim.warp; }
0
java-sources/ai/swim/swim-warp/3.10.0/swim
java-sources/ai/swim/swim-warp/3.10.0/swim/warp/AuthRequest.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.warp; import swim.structure.Form; import swim.structure.Kind; import swim.structure.Value; public final class AuthRequest extends HostAddressed { public AuthRequest(Value body) { super(body); } public AuthRequest() { this(Value.absent()); } @Override public String tag() { return "auth"; } @Override public Form<AuthRequest> form() { return FORM; } @Override public AuthRequest body(Value body) { return new AuthRequest(body); } @Kind public static final Form<AuthRequest> FORM = new AuthRequestForm(); } final class AuthRequestForm extends HostAddressedForm<AuthRequest> { @Override public String tag() { return "auth"; } @Override public Class<?> type() { return AuthRequest.class; } @Override public AuthRequest from(Value body) { return new AuthRequest(body); } }
0
java-sources/ai/swim/swim-warp/3.10.0/swim
java-sources/ai/swim/swim-warp/3.10.0/swim/warp/AuthedResponse.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.warp; import swim.structure.Form; import swim.structure.Kind; import swim.structure.Value; public final class AuthedResponse extends HostAddressed { public AuthedResponse(Value body) { super(body); } public AuthedResponse() { this(Value.absent()); } @Override public String tag() { return "authed"; } @Override public Form<AuthedResponse> form() { return FORM; } @Override public AuthedResponse body(Value body) { return new AuthedResponse(body); } @Kind public static final Form<AuthedResponse> FORM = new AuthedResponseForm(); } final class AuthedResponseForm extends HostAddressedForm<AuthedResponse> { @Override public String tag() { return "authed"; } @Override public Class<?> type() { return AuthedResponse.class; } @Override public AuthedResponse from(Value body) { return new AuthedResponse(body); } }
0
java-sources/ai/swim/swim-warp/3.10.0/swim
java-sources/ai/swim/swim-warp/3.10.0/swim/warp/CommandMessage.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.warp; import swim.structure.Form; import swim.structure.Kind; import swim.structure.Value; import swim.uri.Uri; public final class CommandMessage extends LaneAddressed { public CommandMessage(Uri nodeUri, Uri laneUri, Value body) { super(nodeUri, laneUri, body); } public CommandMessage(Uri nodeUri, Uri laneUri) { this(nodeUri, laneUri, Value.absent()); } public CommandMessage(String nodeUri, String laneUri, Value body) { this(Uri.parse(nodeUri), Uri.parse(laneUri), body); } public CommandMessage(String nodeUri, String laneUri) { this(Uri.parse(nodeUri), Uri.parse(laneUri), Value.absent()); } @Override public String tag() { return "command"; } @Override public Form<CommandMessage> form() { return FORM; } @Override public CommandMessage nodeUri(Uri nodeUri) { return new CommandMessage(nodeUri, this.laneUri, this.body); } @Override public CommandMessage laneUri(Uri laneUri) { return new CommandMessage(this.nodeUri, laneUri, this.body); } @Override public CommandMessage body(Value body) { return new CommandMessage(this.nodeUri, this.laneUri, body); } @Kind public static final Form<CommandMessage> FORM = new CommandMessageForm(); } final class CommandMessageForm extends LaneAddressedForm<CommandMessage> { @Override public String tag() { return "command"; } @Override public Class<?> type() { return CommandMessage.class; } @Override public CommandMessage from(Uri nodeUri, Uri laneUri, Value body) { return new CommandMessage(nodeUri, laneUri, body); } }
0
java-sources/ai/swim/swim-warp/3.10.0/swim
java-sources/ai/swim/swim-warp/3.10.0/swim/warp/DeauthRequest.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.warp; import swim.structure.Form; import swim.structure.Kind; import swim.structure.Value; public final class DeauthRequest extends HostAddressed { public DeauthRequest(Value body) { super(body); } public DeauthRequest() { this(Value.absent()); } @Override public String tag() { return "deauth"; } @Override public Form<DeauthRequest> form() { return FORM; } @Override public DeauthRequest body(Value body) { return new DeauthRequest(body); } @Kind public static final Form<DeauthRequest> FORM = new DeauthRequestForm(); } final class DeauthRequestForm extends HostAddressedForm<DeauthRequest> { @Override public String tag() { return "deauth"; } @Override public Class<?> type() { return DeauthRequest.class; } @Override public DeauthRequest from(Value body) { return new DeauthRequest(body); } }
0
java-sources/ai/swim/swim-warp/3.10.0/swim
java-sources/ai/swim/swim-warp/3.10.0/swim/warp/DeauthedResponse.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.warp; import swim.structure.Form; import swim.structure.Kind; import swim.structure.Value; public final class DeauthedResponse extends HostAddressed { public DeauthedResponse(Value body) { super(body); } public DeauthedResponse() { this(Value.absent()); } @Override public String tag() { return "deauthed"; } @Override public Form<DeauthedResponse> form() { return FORM; } @Override public DeauthedResponse body(Value body) { return new DeauthedResponse(body); } @Kind public static final Form<DeauthedResponse> FORM = new DeauthedResponseForm(); } final class DeauthedResponseForm extends HostAddressedForm<DeauthedResponse> { @Override public String tag() { return "deauthed"; } @Override public Class<?> type() { return DeauthedResponse.class; } @Override public DeauthedResponse from(Value body) { return new DeauthedResponse(body); } }
0
java-sources/ai/swim/swim-warp/3.10.0/swim
java-sources/ai/swim/swim-warp/3.10.0/swim/warp/Envelope.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.warp; import swim.codec.Debug; import swim.codec.Decoder; import swim.codec.Encoder; import swim.codec.Format; import swim.codec.Output; import swim.codec.Writer; import swim.recon.Recon; import swim.structure.Form; import swim.structure.Value; import swim.uri.Uri; public abstract class Envelope implements Debug { Envelope() { // stub } public abstract String tag(); public abstract Form<? extends Envelope> form(); public abstract Uri nodeUri(); public abstract Uri laneUri(); public abstract Value body(); public abstract Envelope nodeUri(Uri node); public abstract Envelope laneUri(Uri lane); public abstract Envelope body(Value body); @SuppressWarnings("unchecked") public Value toValue() { return ((Form<Envelope>) form()).mold(this).toValue(); } public Encoder<?, Envelope> reconEncoder() { return new EnvelopeEncoder(this); } public Writer<?, ?> reconWriter() { return Recon.write(toValue(), Output.full()); } public Writer<?, ?> writeRecon(Output<?> output) { return Recon.write(toValue(), output); } public String toRecon() { return Recon.toString(toValue()); } @Override public abstract void debug(Output<?> output); @Override public String toString() { return Format.debug(this); } private static Decoder<Envelope> decoder; private static Encoder<Envelope, Envelope> encoder; public static Decoder<Envelope> decoder() { if (decoder == null) { decoder = new EnvelopeDecoder(); } return decoder; } public static Encoder<Envelope, Envelope> encoder() { if (encoder == null) { encoder = new EnvelopeEncoder(); } return encoder; } public static Envelope fromValue(Value value) { final String tag = value.tag(); final Form<? extends Envelope> form = form(tag); if (form != null) { return form.cast(value); } else { return null; } } public static Envelope parseRecon(String recon) { final Value value = Recon.parse(recon); return fromValue(value); } @SuppressWarnings("unchecked") public static <E extends Envelope> Form<E> form(String tag) { if ("event".equals(tag)) { return (Form<E>) EventMessage.FORM; } else if ("command".equals(tag)) { return (Form<E>) CommandMessage.FORM; } else if ("link".equals(tag)) { return (Form<E>) LinkRequest.FORM; } else if ("linked".equals(tag)) { return (Form<E>) LinkedResponse.FORM; } else if ("sync".equals(tag)) { return (Form<E>) SyncRequest.FORM; } else if ("synced".equals(tag)) { return (Form<E>) SyncedResponse.FORM; } else if ("unlink".equals(tag)) { return (Form<E>) UnlinkRequest.FORM; } else if ("unlinked".equals(tag)) { return (Form<E>) UnlinkedResponse.FORM; } else if ("auth".equals(tag)) { return (Form<E>) AuthRequest.FORM; } else if ("authed".equals(tag)) { return (Form<E>) AuthedResponse.FORM; } else if ("deauth".equals(tag)) { return (Form<E>) DeauthRequest.FORM; } else if ("deauthed".equals(tag)) { return (Form<E>) DeauthedResponse.FORM; } return null; } }
0
java-sources/ai/swim/swim-warp/3.10.0/swim
java-sources/ai/swim/swim-warp/3.10.0/swim/warp/EnvelopeDecoder.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.warp; import swim.codec.Decoder; import swim.codec.DecoderException; import swim.codec.InputBuffer; import swim.codec.Utf8; import swim.recon.Recon; import swim.structure.Value; final class EnvelopeDecoder extends Decoder<Envelope> { final Decoder<Value> output; EnvelopeDecoder(Decoder<Value> output) { this.output = output; } EnvelopeDecoder() { this(null); } @Override public Decoder<Envelope> feed(InputBuffer input) { return decode(input, this.output); } static Decoder<Envelope> decode(InputBuffer input, Decoder<Value> output) { if (output == null) { output = Utf8.parseDecoded(Recon.structureParser().blockParser(), input); } else { output = output.feed(input); } if (output.isDone()) { try { final Value value = output.bind(); final Envelope envelope = Envelope.fromValue(value); if (envelope != null) { return done(envelope); } else { return error(new DecoderException(Recon.toString(value))); } } catch (RuntimeException cause) { return error(cause); } } else if (output.isError()) { return error(output.trap()); } else if (input.isError()) { return error(input.trap()); } return new EnvelopeDecoder(output); } }
0
java-sources/ai/swim/swim-warp/3.10.0/swim
java-sources/ai/swim/swim-warp/3.10.0/swim/warp/EnvelopeEncoder.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.warp; import swim.codec.Encoder; import swim.codec.OutputBuffer; import swim.codec.Utf8; final class EnvelopeEncoder extends Encoder<Envelope, Envelope> { final Envelope envelope; final Encoder<?, ?> input; EnvelopeEncoder(Envelope envelope, Encoder<?, ?> input) { this.envelope = envelope; this.input = input; } EnvelopeEncoder(Envelope envelope) { this(envelope, null); } EnvelopeEncoder() { this(null, null); } @Override public Encoder<Envelope, Envelope> pull(OutputBuffer<?> output) { return encode(output, this.envelope, this.input); } @Override public Encoder<Envelope, Envelope> feed(Envelope envelope) { return new EnvelopeEncoder(envelope); } static Encoder<Envelope, Envelope> encode(OutputBuffer<?> output, Envelope envelope, Encoder<?, ?> input) { if (input == null) { input = Utf8.writeEncoded(envelope.reconWriter(), output); } else { input = input.pull(output); } if (input.isDone()) { return done(envelope); } else if (input.isError()) { return error(input.trap()); } else if (output.isError()) { return error(output.trap()); } return new EnvelopeEncoder(envelope, input); } }
0
java-sources/ai/swim/swim-warp/3.10.0/swim
java-sources/ai/swim/swim-warp/3.10.0/swim/warp/EventMessage.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.warp; import swim.structure.Form; import swim.structure.Kind; import swim.structure.Value; import swim.uri.Uri; public final class EventMessage extends LaneAddressed { public EventMessage(Uri nodeUri, Uri laneUri, Value body) { super(nodeUri, laneUri, body); } public EventMessage(Uri nodeUri, Uri laneUri) { this(nodeUri, laneUri, Value.absent()); } public EventMessage(String nodeUri, String laneUri, Value body) { this(Uri.parse(nodeUri), Uri.parse(laneUri), body); } public EventMessage(String nodeUri, String laneUri) { this(Uri.parse(nodeUri), Uri.parse(laneUri), Value.absent()); } @Override public String tag() { return "event"; } @Override public Form<EventMessage> form() { return FORM; } @Override public EventMessage nodeUri(Uri nodeUri) { return new EventMessage(nodeUri, this.laneUri, this.body); } @Override public EventMessage laneUri(Uri laneUri) { return new EventMessage(this.nodeUri, laneUri, this.body); } @Override public EventMessage body(Value body) { return new EventMessage(this.nodeUri, this.laneUri, body); } @Kind public static final Form<EventMessage> FORM = new EventMessageForm(); } final class EventMessageForm extends LaneAddressedForm<EventMessage> { @Override public String tag() { return "event"; } @Override public Class<?> type() { return EventMessage.class; } @Override public EventMessage from(Uri nodeUri, Uri laneUri, Value body) { return new EventMessage(nodeUri, laneUri, body); } }
0
java-sources/ai/swim/swim-warp/3.10.0/swim
java-sources/ai/swim/swim-warp/3.10.0/swim/warp/HostAddressed.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.warp; import swim.codec.Output; import swim.structure.Value; import swim.uri.Uri; import swim.util.Murmur3; public abstract class HostAddressed extends Envelope { final Value body; HostAddressed(Value body) { this.body = body.commit(); } @Override public Uri nodeUri() { return Uri.empty(); } @Override public Uri laneUri() { return Uri.empty(); } @Override public Value body() { return this.body; } @Override public HostAddressed nodeUri(Uri nodeUri) { throw new UnsupportedOperationException(); } @Override public HostAddressed laneUri(Uri laneUri) { throw new UnsupportedOperationException(); } @Override public abstract HostAddressed body(Value body); @Override public boolean equals(Object other) { if (this == other) { return true; } else if (other != null && getClass() == other.getClass()) { final HostAddressed that = (HostAddressed) other; return this.body.equals(that.body); } return false; } @Override public int hashCode() { return Murmur3.mash(Murmur3.mix(Murmur3.seed(getClass()), this.body.hashCode())); } @Override public void debug(Output<?> output) { output = output.write("new").write(' ').write(getClass().getSimpleName()).write('('); if (this.body.isDefined()) { output = output.debug(this.body); } output = output.write(')'); } }
0
java-sources/ai/swim/swim-warp/3.10.0/swim
java-sources/ai/swim/swim-warp/3.10.0/swim/warp/HostAddressedForm.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.warp; import swim.structure.Attr; import swim.structure.Form; import swim.structure.Item; import swim.structure.Value; abstract class HostAddressedForm<E extends HostAddressed> extends Form<E> { abstract E from(Value body); @Override public E unit() { return null; } @Override public Item mold(E envelope) { if (envelope != null) { return Attr.of(tag()).concat(envelope.body()); } else { return Item.extant(); } } @Override public E cast(Item item) { final Value value = item.toValue(); if (tag().equals(value.tag())) { final Value body = value.body(); return from(body); } return null; } }
0
java-sources/ai/swim/swim-warp/3.10.0/swim
java-sources/ai/swim/swim-warp/3.10.0/swim/warp/LaneAddressed.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.warp; import swim.codec.Output; import swim.structure.Value; import swim.uri.Uri; import swim.util.Murmur3; public abstract class LaneAddressed extends Envelope { final Uri nodeUri; final Uri laneUri; final Value body; LaneAddressed(Uri nodeUri, Uri laneUri, Value body) { this.nodeUri = nodeUri; this.laneUri = laneUri; this.body = body.commit(); } @Override public Uri nodeUri() { return this.nodeUri; } @Override public Uri laneUri() { return this.laneUri; } @Override public Value body() { return this.body; } @Override public abstract LaneAddressed nodeUri(Uri nodeUri); @Override public abstract LaneAddressed laneUri(Uri laneUri); @Override public abstract LaneAddressed body(Value body); @Override public boolean equals(Object other) { if (this == other) { return true; } else if (other != null && getClass() == other.getClass()) { final LaneAddressed that = (LaneAddressed) other; return this.nodeUri.equals(that.nodeUri) && this.laneUri.equals(that.laneUri) && this.body.equals(that.body); } return false; } @Override public int hashCode() { return Murmur3.mash(Murmur3.mix(Murmur3.mix(Murmur3.mix(Murmur3.seed(getClass()), this.nodeUri.hashCode()), this.laneUri.hashCode()), this.body.hashCode())); } @Override public void debug(Output<?> output) { output = output.write("new").write(' ').write(getClass().getSimpleName()).write('(') .debug(this.nodeUri).write(", ").debug(this.laneUri); if (this.body.isDefined()) { output = output.write(", ").debug(this.body); } output = output.write(')'); } }
0
java-sources/ai/swim/swim-warp/3.10.0/swim
java-sources/ai/swim/swim-warp/3.10.0/swim/warp/LaneAddressedForm.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.warp; import swim.structure.Attr; import swim.structure.Form; import swim.structure.Item; import swim.structure.Record; import swim.structure.Value; import swim.uri.Uri; abstract class LaneAddressedForm<E extends LaneAddressed> extends Form<E> { abstract E from(Uri nodeUri, Uri lane, Value body); @Override public E unit() { return null; } @Override public Item mold(E envelope) { if (envelope != null) { final Record headers = Record.create(2) .slot("node", envelope.nodeUri.toString()) .slot("lane", envelope.laneUri.toString()); return Attr.of(tag(), headers).concat(envelope.body()); } else { return Item.extant(); } } @Override public E cast(Item item) { final Value value = item.toValue(); final Record headers = value.headers(tag()); Uri nodeUri = null; Uri laneUri = null; for (int i = 0, n = headers.size(); i < n; i += 1) { final Item header = headers.get(i); final String key = header.key().stringValue(null); if (key != null) { if ("node".equals(key)) { nodeUri = Uri.parse(header.toValue().stringValue("")); } else if ("lane".equals(key)) { laneUri = Uri.parse(header.toValue().stringValue("")); } } else if (header instanceof Value) { if (i == 0) { nodeUri = Uri.parse(header.stringValue("")); } else if (i == 1) { laneUri = Uri.parse(header.stringValue("")); } } } if (nodeUri != null && laneUri != null) { final Value body = value.body(); return from(nodeUri, laneUri, body); } return null; } }
0
java-sources/ai/swim/swim-warp/3.10.0/swim
java-sources/ai/swim/swim-warp/3.10.0/swim/warp/LinkAddressed.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.warp; import swim.codec.Output; import swim.structure.Value; import swim.uri.Uri; import swim.util.Murmur3; public abstract class LinkAddressed extends LaneAddressed { final float prio; final float rate; LinkAddressed(Uri nodeUri, Uri laneUri, float prio, float rate, Value body) { super(nodeUri, laneUri, body); this.prio = prio; this.rate = rate; } public float prio() { return this.prio; } public float rate() { return this.rate; } @Override public abstract LinkAddressed nodeUri(Uri nodeUri); @Override public abstract LinkAddressed laneUri(Uri laneUri); @Override public abstract LinkAddressed body(Value body); @Override public boolean equals(Object other) { if (this == other) { return true; } else if (other != null && getClass() == other.getClass()) { final LinkAddressed that = (LinkAddressed) other; return this.nodeUri.equals(that.nodeUri) && this.laneUri.equals(that.laneUri) && this.prio == that.prio && this.rate == that.rate && this.body.equals(that.body); } return false; } @Override public int hashCode() { return Murmur3.mash(Murmur3.mix(Murmur3.mix(Murmur3.mix(Murmur3.mix(Murmur3.mix( Murmur3.seed(getClass()), this.nodeUri.hashCode()), this.laneUri.hashCode()), Murmur3.hash(this.prio)), Murmur3.hash(this.rate)), this.body.hashCode())); } @Override public void debug(Output<?> output) { output = output.write("new").write(' ').write(getClass().getSimpleName()).write('(') .debug(this.nodeUri).write(", ").debug(this.laneUri); if (this.prio != 0f || this.rate != 0f) { output = output.write(", ").debug(this.prio).write(", ").debug(this.rate); } if (this.body.isDefined()) { output = output.write(", ").debug(this.body); } output = output.write(')'); } }
0
java-sources/ai/swim/swim-warp/3.10.0/swim
java-sources/ai/swim/swim-warp/3.10.0/swim/warp/LinkAddressedForm.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.warp; import swim.structure.Attr; import swim.structure.Form; import swim.structure.Item; import swim.structure.Record; import swim.structure.Value; import swim.uri.Uri; abstract class LinkAddressedForm<E extends LinkAddressed> extends Form<E> { abstract E from(Uri nodeUri, Uri laneUri, float prio, float rate, Value body); @Override public E unit() { return null; } @Override public Item mold(E envelope) { if (envelope != null) { final Record headers = Record.create(4) .slot("node", envelope.nodeUri.toString()) .slot("lane", envelope.laneUri.toString()); final float prio = envelope.prio; if (prio != 0f && !Float.isNaN(prio)) { headers.slot("prio", prio); } final float rate = envelope.rate; if (rate != 0f && !Float.isNaN(rate)) { headers.slot("rate", rate); } return Attr.of(tag(), headers).concat(envelope.body()); } else { return Item.extant(); } } @Override public E cast(Item item) { final Value value = item.toValue(); final Record headers = value.headers(tag()); Uri nodeUri = null; Uri laneUri = null; float prio = 0f; float rate = 0f; for (int i = 0, n = headers.size(); i < n; i += 1) { final Item header = headers.get(i); final String key = header.key().stringValue(null); if (key != null) { if ("node".equals(key)) { nodeUri = Uri.parse(header.toValue().stringValue("")); } else if ("lane".equals(key)) { laneUri = Uri.parse(header.toValue().stringValue("")); } else if ("prio".equals(key)) { prio = header.toValue().floatValue(); } else if ("rate".equals(key)) { rate = header.toValue().floatValue(); } } else if (header instanceof Value) { if (i == 0) { nodeUri = Uri.parse(header.stringValue("")); } else if (i == 1) { laneUri = Uri.parse(header.stringValue("")); } } } if (nodeUri != null && laneUri != null) { final Value body = value.body(); return from(nodeUri, laneUri, prio, rate, body); } return null; } }
0
java-sources/ai/swim/swim-warp/3.10.0/swim
java-sources/ai/swim/swim-warp/3.10.0/swim/warp/LinkRequest.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.warp; import swim.structure.Form; import swim.structure.Kind; import swim.structure.Value; import swim.uri.Uri; public final class LinkRequest extends LinkAddressed { public LinkRequest(Uri nodeUri, Uri laneUri, float prio, float rate, Value body) { super(nodeUri, laneUri, prio, rate, body); } public LinkRequest(Uri nodeUri, Uri laneUri, float prio, float rate) { this(nodeUri, laneUri, prio, rate, Value.absent()); } public LinkRequest(Uri nodeUri, Uri laneUri, Value body) { this(nodeUri, laneUri, 0f, 0f, body); } public LinkRequest(Uri nodeUri, Uri laneUri) { this(nodeUri, laneUri, 0f, 0f, Value.absent()); } public LinkRequest(String nodeUri, String laneUri, float prio, float rate, Value body) { this(Uri.parse(nodeUri), Uri.parse(laneUri), prio, rate, body); } public LinkRequest(String nodeUri, String laneUri, float prio, float rate) { this(Uri.parse(nodeUri), Uri.parse(laneUri), prio, rate, Value.absent()); } public LinkRequest(String nodeUri, String laneUri, Value body) { this(Uri.parse(nodeUri), Uri.parse(laneUri), 0f, 0f, body); } public LinkRequest(String nodeUri, String laneUri) { this(Uri.parse(nodeUri), Uri.parse(laneUri), 0f, 0f, Value.absent()); } @Override public String tag() { return "link"; } @Override public Form<LinkRequest> form() { return FORM; } @Override public LinkRequest nodeUri(Uri nodeUri) { return new LinkRequest(nodeUri, this.laneUri, this.prio, this.rate, this.body); } @Override public LinkRequest laneUri(Uri laneUri) { return new LinkRequest(this.nodeUri, laneUri, this.prio, this.rate, this.body); } @Override public LinkRequest body(Value body) { return new LinkRequest(this.nodeUri, this.laneUri, this.prio, this.rate, body); } @Kind public static final Form<LinkRequest> FORM = new LinkRequestForm(); } final class LinkRequestForm extends LinkAddressedForm<LinkRequest> { @Override public String tag() { return "link"; } @Override public Class<?> type() { return LinkRequest.class; } @Override public LinkRequest from(Uri nodeUri, Uri laneUri, float prio, float rate, Value body) { return new LinkRequest(nodeUri, laneUri, prio, rate, body); } }
0
java-sources/ai/swim/swim-warp/3.10.0/swim
java-sources/ai/swim/swim-warp/3.10.0/swim/warp/LinkedResponse.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.warp; import swim.structure.Form; import swim.structure.Kind; import swim.structure.Value; import swim.uri.Uri; public final class LinkedResponse extends LinkAddressed { public LinkedResponse(Uri nodeUri, Uri laneUri, float prio, float rate, Value body) { super(nodeUri, laneUri, prio, rate, body); } public LinkedResponse(Uri nodeUri, Uri laneUri, float prio, float rate) { this(nodeUri, laneUri, prio, rate, Value.absent()); } public LinkedResponse(Uri nodeUri, Uri laneUri, Value body) { this(nodeUri, laneUri, 0f, 0f, body); } public LinkedResponse(Uri nodeUri, Uri laneUri) { this(nodeUri, laneUri, 0f, 0f, Value.absent()); } public LinkedResponse(String nodeUri, String laneUri, float prio, float rate, Value body) { this(Uri.parse(nodeUri), Uri.parse(laneUri), prio, rate, body); } public LinkedResponse(String nodeUri, String laneUri, float prio, float rate) { this(Uri.parse(nodeUri), Uri.parse(laneUri), prio, rate, Value.absent()); } public LinkedResponse(String nodeUri, String laneUri, Value body) { this(Uri.parse(nodeUri), Uri.parse(laneUri), 0f, 0f, body); } public LinkedResponse(String nodeUri, String laneUri) { this(Uri.parse(nodeUri), Uri.parse(laneUri), 0f, 0f, Value.absent()); } @Override public String tag() { return "linked"; } @Override public Form<LinkedResponse> form() { return FORM; } @Override public LinkedResponse nodeUri(Uri nodeUri) { return new LinkedResponse(nodeUri, this.laneUri, this.prio, this.rate, this.body); } @Override public LinkedResponse laneUri(Uri laneUri) { return new LinkedResponse(this.nodeUri, laneUri, this.prio, this.rate, this.body); } @Override public LinkedResponse body(Value body) { return new LinkedResponse(this.nodeUri, this.laneUri, this.prio, this.rate, body); } @Kind public static final Form<LinkedResponse> FORM = new LinkedResponseForm(); } final class LinkedResponseForm extends LinkAddressedForm<LinkedResponse> { @Override public String tag() { return "linked"; } @Override public Class<?> type() { return LinkedResponse.class; } @Override public LinkedResponse from(Uri nodeUri, Uri laneUri, float prio, float rate, Value body) { return new LinkedResponse(nodeUri, laneUri, prio, rate, body); } }
0
java-sources/ai/swim/swim-warp/3.10.0/swim
java-sources/ai/swim/swim-warp/3.10.0/swim/warp/SyncRequest.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.warp; import swim.structure.Form; import swim.structure.Kind; import swim.structure.Value; import swim.uri.Uri; public final class SyncRequest extends LinkAddressed { public SyncRequest(Uri nodeUri, Uri laneUri, float prio, float rate, Value body) { super(nodeUri, laneUri, prio, rate, body); } public SyncRequest(Uri nodeUri, Uri laneUri, float prio, float rate) { this(nodeUri, laneUri, prio, rate, Value.absent()); } public SyncRequest(Uri nodeUri, Uri laneUri, Value body) { this(nodeUri, laneUri, 0f, 0f, body); } public SyncRequest(Uri nodeUri, Uri laneUri) { this(nodeUri, laneUri, 0f, 0f, Value.absent()); } public SyncRequest(String nodeUri, String laneUri, float prio, float rate, Value body) { this(Uri.parse(nodeUri), Uri.parse(laneUri), prio, rate, body); } public SyncRequest(String nodeUri, String laneUri, float prio, float rate) { this(Uri.parse(nodeUri), Uri.parse(laneUri), prio, rate, Value.absent()); } public SyncRequest(String nodeUri, String laneUri, Value body) { this(Uri.parse(nodeUri), Uri.parse(laneUri), 0f, 0f, body); } public SyncRequest(String nodeUri, String laneUri) { this(Uri.parse(nodeUri), Uri.parse(laneUri), 0f, 0f, Value.absent()); } @Override public String tag() { return "sync"; } @Override public Form<SyncRequest> form() { return FORM; } @Override public SyncRequest nodeUri(Uri nodeUri) { return new SyncRequest(nodeUri, this.laneUri, this.prio, this.rate, this.body); } @Override public SyncRequest laneUri(Uri laneUri) { return new SyncRequest(this.nodeUri, laneUri, this.prio, this.rate, this.body); } @Override public SyncRequest body(Value body) { return new SyncRequest(this.nodeUri, this.laneUri, this.prio, this.rate, body); } @Kind public static final Form<SyncRequest> FORM = new SyncRequestForm(); } final class SyncRequestForm extends LinkAddressedForm<SyncRequest> { @Override public String tag() { return "sync"; } @Override public Class<?> type() { return SyncRequest.class; } @Override public SyncRequest from(Uri nodeUri, Uri laneUri, float prio, float rate, Value body) { return new SyncRequest(nodeUri, laneUri, prio, rate, body); } }
0
java-sources/ai/swim/swim-warp/3.10.0/swim
java-sources/ai/swim/swim-warp/3.10.0/swim/warp/SyncedResponse.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.warp; import swim.structure.Form; import swim.structure.Kind; import swim.structure.Value; import swim.uri.Uri; public final class SyncedResponse extends LaneAddressed { public SyncedResponse(Uri nodeUri, Uri laneUri, Value body) { super(nodeUri, laneUri, body); } public SyncedResponse(Uri nodeUri, Uri laneUri) { this(nodeUri, laneUri, Value.absent()); } public SyncedResponse(String nodeUri, String laneUri, Value body) { this(Uri.parse(nodeUri), Uri.parse(laneUri), body); } public SyncedResponse(String nodeUri, String laneUri) { this(Uri.parse(nodeUri), Uri.parse(laneUri), Value.absent()); } @Override public String tag() { return "synced"; } @Override public Form<SyncedResponse> form() { return FORM; } @Override public SyncedResponse nodeUri(Uri nodeUri) { return new SyncedResponse(nodeUri, this.laneUri, this.body); } @Override public SyncedResponse laneUri(Uri laneUri) { return new SyncedResponse(this.nodeUri, laneUri, this.body); } @Override public SyncedResponse body(Value body) { return new SyncedResponse(this.nodeUri, this.laneUri, body); } @Kind public static final Form<SyncedResponse> FORM = new SyncedResponseForm(); } final class SyncedResponseForm extends LaneAddressedForm<SyncedResponse> { @Override public String tag() { return "synced"; } @Override public Class<?> type() { return SyncedResponse.class; } @Override public SyncedResponse from(Uri nodeUri, Uri laneUri, Value body) { return new SyncedResponse(nodeUri, laneUri, body); } }
0
java-sources/ai/swim/swim-warp/3.10.0/swim
java-sources/ai/swim/swim-warp/3.10.0/swim/warp/UnlinkRequest.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.warp; import swim.structure.Form; import swim.structure.Kind; import swim.structure.Value; import swim.uri.Uri; public final class UnlinkRequest extends LaneAddressed { public UnlinkRequest(Uri nodeUri, Uri laneUri, Value body) { super(nodeUri, laneUri, body); } public UnlinkRequest(Uri nodeUri, Uri laneUri) { this(nodeUri, laneUri, Value.absent()); } public UnlinkRequest(String nodeUri, String laneUri, Value body) { this(Uri.parse(nodeUri), Uri.parse(laneUri), body); } public UnlinkRequest(String nodeUri, String laneUri) { this(Uri.parse(nodeUri), Uri.parse(laneUri), Value.absent()); } @Override public String tag() { return "unlink"; } @Override public Form<UnlinkRequest> form() { return FORM; } @Override public UnlinkRequest nodeUri(Uri nodeUri) { return new UnlinkRequest(nodeUri, this.laneUri, this.body); } @Override public UnlinkRequest laneUri(Uri laneUri) { return new UnlinkRequest(this.nodeUri, laneUri, this.body); } @Override public UnlinkRequest body(Value body) { return new UnlinkRequest(this.nodeUri, this.laneUri, body); } @Kind public static final Form<UnlinkRequest> FORM = new UnlinkRequestForm(); } final class UnlinkRequestForm extends LaneAddressedForm<UnlinkRequest> { @Override public String tag() { return "unlink"; } @Override public Class<?> type() { return UnlinkRequest.class; } @Override public UnlinkRequest from(Uri nodeUri, Uri laneUri, Value body) { return new UnlinkRequest(nodeUri, laneUri, body); } }
0
java-sources/ai/swim/swim-warp/3.10.0/swim
java-sources/ai/swim/swim-warp/3.10.0/swim/warp/UnlinkedResponse.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.warp; import swim.structure.Form; import swim.structure.Kind; import swim.structure.Value; import swim.uri.Uri; public final class UnlinkedResponse extends LaneAddressed { public UnlinkedResponse(Uri nodeUri, Uri laneUri, Value body) { super(nodeUri, laneUri, body); } public UnlinkedResponse(Uri nodeUri, Uri laneUri) { this(nodeUri, laneUri, Value.absent()); } public UnlinkedResponse(String nodeUri, String laneUri, Value body) { this(Uri.parse(nodeUri), Uri.parse(laneUri), body); } public UnlinkedResponse(String nodeUri, String laneUri) { this(Uri.parse(nodeUri), Uri.parse(laneUri), Value.absent()); } @Override public String tag() { return "unlinked"; } @Override public Form<UnlinkedResponse> form() { return FORM; } @Override public UnlinkedResponse nodeUri(Uri nodeUri) { return new UnlinkedResponse(nodeUri, this.laneUri, this.body); } @Override public UnlinkedResponse laneUri(Uri laneUri) { return new UnlinkedResponse(this.nodeUri, laneUri, this.body); } @Override public UnlinkedResponse body(Value body) { return new UnlinkedResponse(this.nodeUri, this.laneUri, body); } @Kind public static final Form<UnlinkedResponse> FORM = new UnlinkedForm(); } final class UnlinkedForm extends LaneAddressedForm<UnlinkedResponse> { @Override public String tag() { return "unlinked"; } @Override public Class<?> type() { return UnlinkedResponse.class; } @Override public UnlinkedResponse from(Uri nodeUri, Uri laneUri, Value body) { return new UnlinkedResponse(nodeUri, laneUri, body); } }
0
java-sources/ai/swim/swim-warp/3.10.0/swim
java-sources/ai/swim/swim-warp/3.10.0/swim/warp/WarpException.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.warp; public class WarpException extends RuntimeException { private static final long serialVersionUID = 1L; public WarpException(String message, Throwable cause) { super(message, cause); } public WarpException(String message) { super(message); } public WarpException(Throwable cause) { super(cause); } public WarpException() { super(); } }
0
java-sources/ai/swim/swim-warp/3.10.0/swim
java-sources/ai/swim/swim-warp/3.10.0/swim/warp/package-info.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /** * Web Agent Remote Protocol. */ package swim.warp;
0
java-sources/ai/swim/swim-web
java-sources/ai/swim/swim-web/3.10.0/module-info.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /** * Flow-controlled web server library. */ module swim.web { requires transitive swim.io.http; requires transitive swim.io.ws; requires transitive swim.io.warp; exports swim.web; exports swim.web.route; }
0
java-sources/ai/swim/swim-web/3.10.0/swim
java-sources/ai/swim/swim-web/3.10.0/swim/web/WebRequest.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.web; import swim.collections.FingerTrieSeq; import swim.http.HttpEntity; import swim.http.HttpHeader; import swim.http.HttpMethod; import swim.http.HttpRequest; import swim.http.HttpResponse; import swim.http.HttpVersion; import swim.io.http.HttpResponder; import swim.io.http.StaticHttpResponder; import swim.io.warp.WarpSettings; import swim.io.warp.WarpSocket; import swim.io.warp.WarpWebSocket; import swim.io.ws.WebSocket; import swim.io.ws.WsSettings; import swim.io.ws.WsUpgradeResponder; import swim.uri.Uri; import swim.uri.UriPath; import swim.uri.UriQuery; import swim.ws.WsResponse; public abstract class WebRequest { public abstract HttpRequest<?> httpRequest(); public HttpMethod httpMethod() { return httpRequest().method(); } public Uri httpUri() { return httpRequest().uri(); } public UriPath httpUriPath() { return httpUri().path(); } public UriQuery httpUriQuery() { return httpUri().query(); } public HttpVersion httpVersion() { return httpRequest().version(); } public FingerTrieSeq<HttpHeader> httpHeaders() { return httpRequest().headers(); } public HttpHeader getHttpHeader(String name) { return httpRequest().getHeader(name); } public <H extends HttpHeader> H getHttpHeader(Class<H> headerClass) { return httpRequest().getHeader(headerClass); } public HttpEntity<?> httpEntity() { return httpRequest().entity(); } public abstract UriPath routePath(); public abstract WebRequest routePath(UriPath routePath); public WebResponse accept(HttpResponder<?> responder) { return new WebServerAccepted(responder); } public WebResponse respond(HttpResponse<?> response) { return accept(new StaticHttpResponder<>(response)); } public WebResponse upgrade(WebSocket<?, ?> webSocket, WsResponse wsResponse, WsSettings wsSettings) { return accept(new WsUpgradeResponder(webSocket, wsResponse, wsSettings)); } public WebResponse upgrade(WarpSocket warpSocket, WsResponse wsResponse, WarpSettings warpSettings) { final WarpWebSocket webSocket = new WarpWebSocket(warpSocket, warpSettings); warpSocket.setWarpSocketContext(webSocket); // eagerly set return accept(new WsUpgradeResponder(webSocket, wsResponse, warpSettings.wsSettings())); } public WebResponse reject(HttpResponder<?> responder) { return new WebServerRejected(responder); } public WebResponse reject() { return WebServerRejected.notFound(); } }
0
java-sources/ai/swim/swim-web/3.10.0/swim
java-sources/ai/swim/swim-web/3.10.0/swim/web/WebResponse.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.web; import swim.io.http.HttpResponder; public abstract class WebResponse { public abstract boolean isAccepted(); public abstract boolean isRejected(); public abstract HttpResponder<?> httpResponder(); }
0
java-sources/ai/swim/swim-web/3.10.0/swim
java-sources/ai/swim/swim-web/3.10.0/swim/web/WebRoute.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.web; import swim.web.route.AlternativeRoute; @FunctionalInterface public interface WebRoute { WebResponse routeRequest(WebRequest request); default WebRoute orElse(WebRoute alternative) { return new AlternativeRoute(this, alternative); } }
0
java-sources/ai/swim/swim-web/3.10.0/swim
java-sources/ai/swim/swim-web/3.10.0/swim/web/WebServerAccepted.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.web; import swim.io.http.HttpResponder; final class WebServerAccepted extends WebResponse { final HttpResponder<?> httpResponder; WebServerAccepted(HttpResponder<?> httpResponder) { this.httpResponder = httpResponder; } @Override public boolean isAccepted() { return true; } @Override public boolean isRejected() { return false; } @Override public HttpResponder<?> httpResponder() { return this.httpResponder; } }
0
java-sources/ai/swim/swim-web/3.10.0/swim
java-sources/ai/swim/swim-web/3.10.0/swim/web/WebServerRejected.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.web; import swim.http.HttpBody; import swim.http.HttpResponse; import swim.http.HttpStatus; import swim.io.http.HttpResponder; import swim.io.http.StaticHttpResponder; final class WebServerRejected extends WebResponse { final HttpResponder<?> httpResponder; WebServerRejected(HttpResponder<?> httpResponder) { this.httpResponder = httpResponder; } @Override public boolean isAccepted() { return false; } @Override public boolean isRejected() { return true; } @Override public HttpResponder<?> httpResponder() { return this.httpResponder; } private static WebServerRejected notFound; static WebServerRejected notFound() { if (notFound == null) { final HttpResponse<Object> response = HttpResponse.from(HttpStatus.NOT_FOUND).content(HttpBody.empty()); final HttpResponder<Object> responder = new StaticHttpResponder<Object>(response); notFound = new WebServerRejected(responder); } return notFound; } }
0
java-sources/ai/swim/swim-web/3.10.0/swim
java-sources/ai/swim/swim-web/3.10.0/swim/web/WebServerRequest.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.web; import swim.http.HttpRequest; import swim.uri.UriPath; public class WebServerRequest extends WebRequest { final HttpRequest<?> httpRequest; final UriPath routePath; public WebServerRequest(HttpRequest<?> httpRequest, UriPath routePath) { this.httpRequest = httpRequest; this.routePath = routePath; } public WebServerRequest(HttpRequest<?> httpRequest) { this(httpRequest, httpRequest.uri().path()); } @Override public final HttpRequest<?> httpRequest() { return this.httpRequest; } @Override public final UriPath routePath() { return this.routePath; } @Override public WebRequest routePath(UriPath routePath) { return new WebServerRequest(this.httpRequest, routePath); } }
0
java-sources/ai/swim/swim-web/3.10.0/swim
java-sources/ai/swim/swim-web/3.10.0/swim/web/package-info.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /** * Flow-controlled web server library. */ package swim.web;
0
java-sources/ai/swim/swim-web/3.10.0/swim/web
java-sources/ai/swim/swim-web/3.10.0/swim/web/route/AlternativeRoute.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.web.route; import swim.web.WebRequest; import swim.web.WebResponse; import swim.web.WebRoute; public final class AlternativeRoute implements WebRoute { final WebRoute[] routes; public AlternativeRoute(WebRoute... routes) { this.routes = routes; } @Override public WebResponse routeRequest(WebRequest request) { final WebRoute[] routes = this.routes; for (int i = 0, n = routes.length; i < n; i += 1) { final WebRoute route = routes[i]; final WebResponse response = route.routeRequest(request); if (response.isAccepted()) { return response; } } return request.reject(); } @Override public WebRoute orElse(WebRoute alternative) { final WebRoute[] oldRoutes = this.routes; final int n = oldRoutes.length; final WebRoute[] newRoutes = new WebRoute[n + 1]; System.arraycopy(oldRoutes, 0, newRoutes, 0, n); newRoutes[n] = alternative; return new AlternativeRoute(newRoutes); } }
0
java-sources/ai/swim/swim-web/3.10.0/swim/web
java-sources/ai/swim/swim-web/3.10.0/swim/web/route/DirectoryRoute.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.web.route; import java.io.IOException; import swim.http.HttpBody; import swim.http.HttpResponse; import swim.http.HttpStatus; import swim.uri.UriPath; import swim.web.WebRequest; import swim.web.WebResponse; import swim.web.WebRoute; public final class DirectoryRoute implements WebRoute { final UriPath directory; final String indexFile; public DirectoryRoute(UriPath directory, String indexFile) { this.directory = directory; this.indexFile = indexFile; } @Override public WebResponse routeRequest(WebRequest request) { UriPath path = request.routePath(); if (path.foot().isAbsolute()) { path = path.appended(this.indexFile); } if (path.isAbsolute()) { path = path.tail(); } path = this.directory.appended(path).removeDotSegments(); if (path.isSubpathOf(this.directory)) { try { final HttpBody<Object> body = HttpBody.fromFile(path.toString()); final HttpResponse<Object> response = HttpResponse.from(HttpStatus.OK).content(body); return request.respond(response); } catch (IOException error) { return request.reject(); } } else { return request.reject(); } } }
0
java-sources/ai/swim/swim-web/3.10.0/swim/web
java-sources/ai/swim/swim-web/3.10.0/swim/web/route/PathPrefixRoute.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.web.route; import swim.uri.UriPath; import swim.web.WebRequest; import swim.web.WebResponse; import swim.web.WebRoute; public final class PathPrefixRoute implements WebRoute { final UriPath pathPrefix; final WebRoute then; public PathPrefixRoute(UriPath pathPrefix, WebRoute then) { this.pathPrefix = pathPrefix; this.then = then; } @Override public WebResponse routeRequest(WebRequest request) { UriPath pathPrefix = this.pathPrefix; UriPath routePath = request.routePath(); while (!pathPrefix.isEmpty() && !routePath.isEmpty()) { if (!pathPrefix.head().equals(routePath.head())) { return request.reject(); } pathPrefix = pathPrefix.tail(); routePath = routePath.tail(); } if (pathPrefix.isEmpty()) { return this.then.routeRequest(request.routePath(routePath)); } else { return request.reject(); } } }
0
java-sources/ai/swim/swim-web/3.10.0/swim/web
java-sources/ai/swim/swim-web/3.10.0/swim/web/route/RejectRoute.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.web.route; import swim.web.WebRequest; import swim.web.WebResponse; import swim.web.WebRoute; public final class RejectRoute implements WebRoute { @Override public WebResponse routeRequest(WebRequest request) { return request.reject(); } @Override public WebRoute orElse(WebRoute alternative) { return alternative; } }
0
java-sources/ai/swim/swim-web/3.10.0/swim/web
java-sources/ai/swim/swim-web/3.10.0/swim/web/route/ResourceDirectoryRoute.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.web.route; import java.io.IOException; import swim.http.HttpBody; import swim.http.HttpResponse; import swim.http.HttpStatus; import swim.uri.UriPath; import swim.web.WebRequest; import swim.web.WebResponse; import swim.web.WebRoute; public final class ResourceDirectoryRoute implements WebRoute { final ClassLoader classLoader; final UriPath directory; final String indexFile; public ResourceDirectoryRoute(ClassLoader classLoader, UriPath directory, String indexFile) { this.classLoader = classLoader; this.directory = directory; this.indexFile = indexFile; } @Override public WebResponse routeRequest(WebRequest request) { UriPath path = request.routePath(); if (path.foot().isAbsolute()) { path = path.appended(this.indexFile); } if (path.isAbsolute()) { path = path.tail(); } path = this.directory.appended(path).removeDotSegments(); if (path.isSubpathOf(this.directory)) { try { final HttpBody<Object> body = HttpBody.fromResource(this.classLoader, path.toString()); if (body != null) { final HttpResponse<Object> response = HttpResponse.from(HttpStatus.OK).content(body); return request.respond(response); } } catch (IOException error) { // continue } } return request.reject(); } }
0
java-sources/ai/swim/swim-web/3.10.0/swim/web
java-sources/ai/swim/swim-web/3.10.0/swim/web/route/package-info.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /** * Web server routing directives. */ package swim.web.route;
0
java-sources/ai/swim/swim-ws
java-sources/ai/swim/swim-ws/3.10.0/module-info.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /** * WebSocket wire protocol model, decoders, and encoders. */ module swim.ws { requires swim.util; requires transitive swim.codec; requires transitive swim.structure; requires transitive swim.collections; requires transitive swim.uri; requires transitive swim.deflate; requires transitive swim.http; exports swim.ws; }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/Ws.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import swim.deflate.Deflate; import swim.deflate.Inflate; public final class Ws { private Ws() { // stub } private static WsDecoder standardDecoder; private static WsEncoder standardEncoderMasked; private static WsEncoder standardEncoderUnmasked; public static WsDecoder standardDecoder() { if (standardDecoder == null) { standardDecoder = new WsStandardDecoder(); } return standardDecoder; } public static WsEncoder standardEncoderMasked() { if (standardEncoderMasked == null) { standardEncoderMasked = new WsStandardEncoderMasked(); } return standardEncoderMasked; } public static WsEncoder standardEncoderUnmasked() { if (standardEncoderUnmasked == null) { standardEncoderUnmasked = new WsStandardEncoderUnmasked(); } return standardEncoderUnmasked; } public static WsDeflateDecoder deflateDecoder(Inflate<?> inflate) { return new WsDeflateDecoder(inflate); } public static WsDeflateDecoder deflateDecoder() { return new WsDeflateDecoder(new Inflate<Object>()); } public static WsDeflateEncoder deflateEncoderMasked(Deflate<?> deflate, int flush) { return new WsDeflateEncoderMasked(deflate, flush); } public static WsDeflateEncoder deflateEncoderMasked() { return new WsDeflateEncoderMasked(new Deflate<Object>(), Deflate.Z_SYNC_FLUSH); } public static WsDeflateEncoder deflateEncoderUnmasked(Deflate<?> deflate, int flush) { return new WsDeflateEncoderUnmasked(deflate, flush); } public static WsDeflateEncoder deflateEncoderUnmasked() { return new WsDeflateEncoderUnmasked(new Deflate<Object>(), Deflate.Z_SYNC_FLUSH); } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsBinary.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import java.nio.ByteBuffer; import swim.codec.Binary; import swim.codec.Debug; import swim.codec.Encoder; import swim.codec.Format; import swim.codec.Output; import swim.codec.OutputBuffer; import swim.structure.Data; import swim.util.Murmur3; public final class WsBinary<T> extends WsData<T> implements Debug { final T value; final Encoder<?, ?> content; WsBinary(T value, Encoder<?, ?> content) { this.value = value; this.content = content; } @Override public boolean isDefined() { return this.value != null; } @Override public T get() { return this.value; } @Override public WsOpcode opcode() { return WsOpcode.BINARY; } @Override public Object payload() { return this.value; } @Override public Encoder<?, ?> contentEncoder(WsEncoder ws) { return this.content; } @Override public Encoder<?, ?> encodeContent(OutputBuffer<?> output, WsEncoder ws) { return this.content.pull(output); } @Override public boolean equals(Object other) { if (this == other) { return true; } else if (other instanceof WsBinary<?>) { final WsBinary<?> that = (WsBinary<?>) other; return (this.value == null ? that.value == null : this.value.equals(that.value)); } return false; } @Override public int hashCode() { if (hashSeed == 0) { hashSeed = Murmur3.seed(WsBinary.class); } return Murmur3.mash(Murmur3.mix(hashSeed, Murmur3.hash(this.value))); } @Override public void debug(Output<?> output) { output = output.write("WsBinary").write('.').write("from").write('('); if (this.value != null) { output = output.debug(this.value).write(", "); } output = output.debug(this.content).write(')'); } @Override public String toString() { return Format.debug(this); } private static int hashSeed; public static <T> WsBinary<T> from(T value, Encoder<?, ?> content) { return new WsBinary<T>(value, content); } public static <T> WsBinary<T> from(Encoder<?, ?> content) { return new WsBinary<T>(null, content); } public static WsBinary<ByteBuffer> from(ByteBuffer payload) { return new WsBinary<ByteBuffer>(payload.duplicate(), Binary.byteBufferWriter(payload)); } public static WsBinary<Data> from(Data payload) { return new WsBinary<Data>(payload, payload.writer()); } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsClose.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import swim.codec.Debug; import swim.codec.Encoder; import swim.codec.Format; import swim.codec.Output; import swim.codec.OutputBuffer; import swim.util.Murmur3; public final class WsClose<P, T> extends WsControl<P, T> implements Debug { final P payload; final Encoder<?, ?> content; WsClose(P payload, Encoder<?, ?> content) { this.payload = payload; this.content = content; } @Override public WsOpcode opcode() { return WsOpcode.CLOSE; } @Override public P payload() { return this.payload; } @Override public Encoder<?, ?> contentEncoder(WsEncoder ws) { return this.content; } @Override public Encoder<?, ?> encodeContent(OutputBuffer<?> output, WsEncoder ws) { return this.content.pull(output); } @Override public boolean equals(Object other) { if (this == other) { return true; } else if (other instanceof WsClose<?, ?>) { final WsClose<?, ?> that = (WsClose<?, ?>) other; return (this.payload == null ? that.payload == null : this.payload.equals(that.payload)); } return false; } @Override public int hashCode() { if (hashSeed == 0) { hashSeed = Murmur3.seed(WsClose.class); } return Murmur3.mash(Murmur3.mix(hashSeed, Murmur3.hash(this.payload))); } @Override public void debug(Output<?> output) { output = output.write("WsClose").write('.').write("from").write('(') .debug(this.payload).write(", ").debug(this.content).write(')'); } @Override public String toString() { return Format.debug(this); } private static int hashSeed; public static <P, T> WsClose<P, T> empty() { return new WsClose<P, T>(null, Encoder.done()); } public static <P, T> WsClose<P, T> from(P payload, Encoder<?, ?> content) { return new WsClose<P, T>(payload, content); } @SuppressWarnings("unchecked") public static <P, T> WsClose<P, T> from(P payload) { if (payload instanceof WsStatus) { return (WsClose<P, T>) from((WsStatus) payload); } else { return new WsClose<P, T>(payload, Encoder.done()); } } public static <T> WsClose<WsStatus, T> from(WsStatus status) { return new WsClose<WsStatus, T>(status, status.encoder()); } public static <T> WsClose<WsStatus, T> from(int code, String reason) { return from(WsStatus.from(code, reason)); } public static <T> WsClose<WsStatus, T> from(int code) { return from(WsStatus.from(code)); } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsControl.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; public abstract class WsControl<P, T> extends WsFrame<T> { WsControl() { // stub } @Override public boolean isDefined() { return false; } @Override public T get() { return null; } @Override public abstract P payload(); }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsData.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; public abstract class WsData<T> extends WsFrame<T> { public WsData() { // stub } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsDecoder.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import swim.codec.Binary; import swim.codec.Decoder; import swim.codec.DecoderException; import swim.codec.InputBuffer; import swim.structure.Data; public abstract class WsDecoder { public <T> WsFrame<T> fragment(WsOpcode opcode, Decoder<T> content) { return new WsFragment<T>(opcode, content); } public <T> WsFrame<T> message(T value) { return new WsValue<T>(value); } public <P, T> WsFrame<T> control(WsOpcode opcode, P payload) { switch (opcode) { case CLOSE: return close(payload); case PING: return ping(payload); case PONG: return pong(payload); default: throw new IllegalArgumentException(opcode.toString()); } } @SuppressWarnings("unchecked") public <P, T> WsFrame<T> close(P payload) { return (WsFrame<T>) WsClose.from(payload); } @SuppressWarnings("unchecked") public <P, T> WsFrame<T> ping(P payload) { return (WsFrame<T>) WsPing.from(payload); } @SuppressWarnings("unchecked") public <P, T> WsFrame<T> pong(P payload) { return (WsFrame<T>) WsPong.from(payload); } public <T> Decoder<T> continuationDecoder(Decoder<T> content) { return content; } public <T> Decoder<T> textDecoder(Decoder<T> content) { return content.fork(WsOpcode.TEXT); } public <T> Decoder<T> binaryDecoder(Decoder<T> content) { return content.fork(WsOpcode.BINARY); } public <T> Decoder<?> closeDecoder(Decoder<T> content) { return WsStatus.decoder(); } public <T> Decoder<?> pingDecoder(Decoder<T> content) { return Binary.outputParser(Data.output()); } public <T> Decoder<?> pongDecoder(Decoder<T> content) { return Binary.outputParser(Data.output()); } public <T> Decoder<WsFrame<T>> frameDecoder(Decoder<T> content) { return new WsOpcodeDecoder<T>(this, content); } public <T> Decoder<WsFrame<T>> decodeFrame(Decoder<T> content, InputBuffer input) { return WsOpcodeDecoder.decode(input, this, content); } public <T> Decoder<WsFrame<T>> decodeFrame(int finRsvOp, Decoder<T> content, InputBuffer input) { final int opcode = finRsvOp & 0xf; switch (opcode) { case 0x0: return decodeContinuationFrame(finRsvOp, continuationDecoder(content), input); case 0x1: return decodeTextFrame(finRsvOp, textDecoder(content), input); case 0x2: return decodeBinaryFrame(finRsvOp, binaryDecoder(content), input); case 0x8: return decodeCloseFrame(finRsvOp, closeDecoder(content), input); case 0x9: return decodePingFrame(finRsvOp, pingDecoder(content), input); case 0xa: return decodePongFrame(finRsvOp, pongDecoder(content), input); default: return Decoder.error(new DecoderException("reserved opcode: " + WsOpcode.from(opcode))); } } public <T> Decoder<WsFrame<T>> decodeContinuationFrame(int finRsvOp, Decoder<T> content, InputBuffer input) { return WsFrameDecoder.decode(input, this, content); } public <T> Decoder<WsFrame<T>> decodeTextFrame(int finRsvOp, Decoder<T> content, InputBuffer input) { return WsFrameDecoder.decode(input, this, content); } public <T> Decoder<WsFrame<T>> decodeBinaryFrame(int finRsvOp, Decoder<T> content, InputBuffer input) { return WsFrameDecoder.decode(input, this, content); } @SuppressWarnings("unchecked") public <P, T> Decoder<WsFrame<T>> decodeCloseFrame(int finRsvOp, Decoder<P> content, InputBuffer input) { return (Decoder<WsFrame<T>>) (Decoder<?>) WsFrameDecoder.decode(input, this, content); } @SuppressWarnings("unchecked") public <P, T> Decoder<WsFrame<T>> decodePingFrame(int finRsvOp, Decoder<P> content, InputBuffer input) { return (Decoder<WsFrame<T>>) (Decoder<?>) WsFrameDecoder.decode(input, this, content); } @SuppressWarnings("unchecked") public <P, T> Decoder<WsFrame<T>> decodePongFrame(int finRsvOp, Decoder<P> content, InputBuffer input) { return (Decoder<WsFrame<T>>) (Decoder<?>) WsFrameDecoder.decode(input, this, content); } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsDeflateClientEngine.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import swim.deflate.Deflate; import swim.deflate.Inflate; import swim.http.WebSocketExtension; import swim.http.WebSocketParam; final class WsDeflateClientEngine extends WsEngine { protected final int clientCompressionLevel; protected final boolean clientNoContextTakeover; protected final int serverMaxWindowBits; protected final int clientMaxWindowBits; WsDeflateClientEngine(int clientCompressionLevel, boolean clientNoContextTakeover, int serverMaxWindowBits, int clientMaxWindowBits) { this.clientCompressionLevel = clientCompressionLevel; this.clientNoContextTakeover = clientNoContextTakeover; this.serverMaxWindowBits = serverMaxWindowBits; this.clientMaxWindowBits = clientMaxWindowBits; } @Override public WsDecoder decoder() { return Ws.deflateDecoder(new Inflate<Object>(Inflate.Z_NO_WRAP, this.serverMaxWindowBits)); } @Override public WsEncoder encoder() { final int flush; if (this.clientNoContextTakeover) { flush = Deflate.Z_FULL_FLUSH; } else { flush = Deflate.Z_SYNC_FLUSH; } return Ws.deflateEncoderMasked(new Deflate<Object>(Deflate.Z_NO_WRAP, this.clientCompressionLevel, this.clientMaxWindowBits), flush); } @Override public WsEngine extension(WebSocketExtension extension, WsEngineSettings settings) { return this; } static WsDeflateClientEngine from(WebSocketExtension extension, WsEngineSettings settings) { boolean clientNoContextTakeover = false; int serverMaxWindowBits = 15; int clientMaxWindowBits = 15; for (WebSocketParam param : extension.params()) { final String key = param.key(); final String value = param.value(); if ("client_no_context_takeover".equals(key)) { clientNoContextTakeover = true; } else if ("server_max_window_bits".equals(key)) { try { serverMaxWindowBits = Integer.parseInt(value); } catch (NumberFormatException cause) { throw new WsException("invalid permessage-deflate; " + param.toHttp()); } } else if ("client_max_window_bits".equals(key)) { try { clientMaxWindowBits = Integer.parseInt(value); } catch (NumberFormatException cause) { throw new WsException("invalid permessage-deflate; " + param.toHttp()); } } else { throw new WsException("invalid permessage-deflate; " + param.toHttp()); } } return new WsDeflateClientEngine(settings.clientCompressionLevel, clientNoContextTakeover, serverMaxWindowBits, clientMaxWindowBits); } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsDeflateDecoder.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import swim.codec.Decoder; import swim.codec.InputBuffer; import swim.deflate.Inflate; public class WsDeflateDecoder extends WsDecoder implements Cloneable { protected final Inflate<?> inflate; protected boolean decompressing; public WsDeflateDecoder(Inflate<?> inflate, boolean decompressing) { this.inflate = inflate; this.decompressing = decompressing; } public WsDeflateDecoder(Inflate<?> inflate) { this(inflate, false); } public final Inflate<?> inflate() { return this.inflate; } public final boolean decompressing() { return this.decompressing; } @Override public <T> Decoder<WsFrame<T>> decodeContinuationFrame(int finRsvOp, Decoder<T> content, InputBuffer input) { if (decompressing) { // compressed return WsFrameInflater.decode(input, this, content); } else { // uncompressed return WsFrameDecoder.decode(input, this, content); } } @Override public <T> Decoder<WsFrame<T>> decodeTextFrame(int finRsvOp, Decoder<T> content, InputBuffer input) { if ((finRsvOp & 0x40) != 0) { // compressed this.decompressing = (finRsvOp & 0x80) == 0; return WsFrameInflater.decode(input, this, content); } else { // uncompressed this.decompressing = false; return WsFrameDecoder.decode(input, this, content); } } @Override public <T> Decoder<WsFrame<T>> decodeBinaryFrame(int finRsvOp, Decoder<T> content, InputBuffer input) { if ((finRsvOp & 0x40) != 0) { // compressed this.decompressing = (finRsvOp & 0x80) == 0; return WsFrameInflater.decode(input, this, content); } else { // uncompressed this.decompressing = false; return WsFrameDecoder.decode(input, this, content); } } @Override public WsDeflateDecoder clone() { return new WsDeflateDecoder(this.inflate.clone(), this.decompressing); } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsDeflateEncoder.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import swim.codec.Encoder; import swim.codec.OutputBuffer; import swim.deflate.Deflate; public abstract class WsDeflateEncoder extends WsEncoder { protected final Deflate<?> deflate; protected final int flush; public WsDeflateEncoder(Deflate<?> deflate, int flush) { this.deflate = deflate; this.flush = flush; } public final Deflate<?> deflate() { return this.deflate; } public final int flush() { return this.flush; } @Override public <T> Encoder<?, WsFrame<T>> textFrameEncoder(WsFrame<T> frame) { return new WsFrameDeflater<T>(this, frame); } @Override public <T> Encoder<?, WsFrame<T>> encodeTextFrame(WsFrame<T> frame, OutputBuffer<?> output) { return WsFrameDeflater.encode(output, this, frame); } @Override public <T> Encoder<?, WsFrame<T>> binaryFrameEncoder(WsFrame<T> frame) { return new WsFrameDeflater<T>(this, frame); } @Override public <T> Encoder<?, WsFrame<T>> encodeBinaryFrame(WsFrame<T> frame, OutputBuffer<?> output) { return WsFrameDeflater.encode(output, this, frame); } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsDeflateEncoderMasked.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import java.util.concurrent.ThreadLocalRandom; import swim.deflate.Deflate; final class WsDeflateEncoderMasked extends WsDeflateEncoder { WsDeflateEncoderMasked(Deflate<?> deflate, int flush) { super(deflate, flush); } @Override public boolean isMasked() { return true; } @Override public void maskingKey(byte[] maskingKey) { ThreadLocalRandom.current().nextBytes(maskingKey); } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsDeflateEncoderUnmasked.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import swim.deflate.Deflate; final class WsDeflateEncoderUnmasked extends WsDeflateEncoder { WsDeflateEncoderUnmasked(Deflate<?> deflate, int flush) { super(deflate, flush); } @Override public boolean isMasked() { return false; } @Override public void maskingKey(byte[] maskingKey) { // nop } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsDeflateServerEngine.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import swim.deflate.Deflate; import swim.deflate.Inflate; import swim.http.WebSocketExtension; import swim.http.WebSocketParam; final class WsDeflateServerEngine extends WsEngine { protected final int serverCompressionLevel; protected final boolean serverNoContextTakeover; protected final int serverMaxWindowBits; protected final int clientMaxWindowBits; WsDeflateServerEngine(int serverCompressionLevel, boolean serverNoContextTakeover, int serverMaxWindowBits, int clientMaxWindowBits) { this.serverCompressionLevel = serverCompressionLevel; this.serverNoContextTakeover = serverNoContextTakeover; this.serverMaxWindowBits = serverMaxWindowBits; this.clientMaxWindowBits = clientMaxWindowBits; } @Override public WsDecoder decoder() { return Ws.deflateDecoder(new Inflate<Object>(Inflate.Z_NO_WRAP, this.clientMaxWindowBits)); } @Override public WsEncoder encoder() { final int flush; if (serverNoContextTakeover) { flush = Deflate.Z_FULL_FLUSH; } else { flush = Deflate.Z_SYNC_FLUSH; } return Ws.deflateEncoderUnmasked(new Deflate<Object>(Deflate.Z_NO_WRAP, this.serverCompressionLevel, this.serverMaxWindowBits), flush); } @Override public WsEngine extension(WebSocketExtension extension, WsEngineSettings settings) { return this; } static WsDeflateServerEngine from(WebSocketExtension extension, WsEngineSettings settings) { boolean serverNoContextTakeover = false; int serverMaxWindowBits = 15; int clientMaxWindowBits = 15; for (WebSocketParam param : extension.params()) { final String key = param.key(); final String value = param.value(); if ("server_no_context_takeover".equals(key)) { serverNoContextTakeover = true; } else if ("server_max_window_bits".equals(key)) { try { serverMaxWindowBits = Integer.parseInt(value); } catch (NumberFormatException cause) { throw new WsException("invalid permessage-deflate; " + param.toHttp()); } } else if ("client_max_window_bits".equals(key)) { try { clientMaxWindowBits = Integer.parseInt(value); } catch (NumberFormatException cause) { throw new WsException("invalid permessage-deflate; " + param.toHttp()); } } else { throw new WsException("invalid permessage-deflate; " + param.toHttp()); } } return new WsDeflateServerEngine(settings.serverCompressionLevel, serverNoContextTakeover, serverMaxWindowBits, clientMaxWindowBits); } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsEncoder.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import swim.codec.Encoder; import swim.codec.EncoderException; import swim.codec.OutputBuffer; public abstract class WsEncoder { public abstract boolean isMasked(); public abstract void maskingKey(byte[] maskingKey); public <T> Encoder<?, WsFrame<T>> frameEncoder(WsFrame<T> frame) { final WsOpcode opcode = frame.opcode(); switch (opcode) { case CONTINUATION: return Encoder.error(new EncoderException("invalid opcode: " + opcode)); case TEXT: return textFrameEncoder(frame); case BINARY: return binaryFrameEncoder(frame); case CLOSE: return closeFrameEncoder(frame); case PING: return pingFrameEncoder(frame); case PONG: return pongFrameEncoder(frame); default: return Encoder.error(new EncoderException("reserved opcode: " + opcode)); } } public <T> Encoder<?, WsFrame<T>> encodeFrame(WsFrame<T> frame, OutputBuffer<?> output) { final WsOpcode opcode = frame.opcode(); switch (opcode) { case CONTINUATION: return Encoder.error(new EncoderException("invalid opcode: " + opcode)); case TEXT: return encodeTextFrame(frame, output); case BINARY: return encodeBinaryFrame(frame, output); case CLOSE: return encodeCloseFrame(frame, output); case PING: return encodePingFrame(frame, output); case PONG: return encodePongFrame(frame, output); default: return Encoder.error(new EncoderException("reserved opcode: " + opcode)); } } public <T> Encoder<?, WsFrame<T>> textFrameEncoder(WsFrame<T> frame) { return new WsFrameEncoder<T>(this, frame); } public <T> Encoder<?, WsFrame<T>> encodeTextFrame(WsFrame<T> frame, OutputBuffer<?> output) { return WsFrameEncoder.encode(output, this, frame); } public <T> Encoder<?, WsFrame<T>> binaryFrameEncoder(WsFrame<T> frame) { return new WsFrameEncoder<T>(this, frame); } public <T> Encoder<?, WsFrame<T>> encodeBinaryFrame(WsFrame<T> frame, OutputBuffer<?> output) { return WsFrameEncoder.encode(output, this, frame); } public <T> Encoder<?, WsFrame<T>> closeFrameEncoder(WsFrame<T> frame) { return new WsFrameEncoder<T>(this, frame); } public <T> Encoder<?, WsFrame<T>> encodeCloseFrame(WsFrame<T> frame, OutputBuffer<?> output) { return WsFrameEncoder.encode(output, this, frame); } public <T> Encoder<?, WsFrame<T>> pingFrameEncoder(WsFrame<T> frame) { return new WsFrameEncoder<T>(this, frame); } public <T> Encoder<?, WsFrame<T>> encodePingFrame(WsFrame<T> frame, OutputBuffer<?> output) { return WsFrameEncoder.encode(output, this, frame); } public <T> Encoder<?, WsFrame<T>> pongFrameEncoder(WsFrame<T> frame) { return new WsFrameEncoder<T>(this, frame); } public <T> Encoder<?, WsFrame<T>> encodePongFrame(WsFrame<T> frame, OutputBuffer<?> output) { return WsFrameEncoder.encode(output, this, frame); } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsEngine.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import swim.http.WebSocketExtension; public abstract class WsEngine { public abstract WsDecoder decoder(); public abstract WsEncoder encoder(); public abstract WsEngine extension(WebSocketExtension extension, WsEngineSettings settings); public WsEngine extensions(Iterable<WebSocketExtension> extensions, WsEngineSettings settings) { WsEngine engine = this; for (WebSocketExtension extension : extensions) { engine = engine.extension(extension, settings); } return engine; } private static WsEngine standardClientEngine; private static WsEngine standardServerEngine; public static WsEngine standardClientEngine() { if (standardClientEngine == null) { standardClientEngine = new WsStandardClientEngine(); } return standardClientEngine; } public static WsEngine standardServerEngine() { if (standardServerEngine == null) { standardServerEngine = new WsStandardServerEngine(); } return standardServerEngine; } public static WsEngine deflateClientEngine(WebSocketExtension extension, WsEngineSettings settings) { return WsDeflateClientEngine.from(extension, settings); } public static WsEngine deflateServerEngine(WebSocketExtension extension, WsEngineSettings settings) { return WsDeflateServerEngine.from(extension, settings); } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsEngineSettings.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import swim.codec.Debug; import swim.codec.Format; import swim.codec.Output; import swim.collections.FingerTrieSeq; import swim.http.HttpHeader; import swim.http.WebSocketExtension; import swim.http.WebSocketParam; import swim.structure.Form; import swim.structure.Item; import swim.structure.Kind; import swim.structure.Record; import swim.structure.Value; import swim.uri.Uri; import swim.util.Murmur3; public class WsEngineSettings implements Debug { protected final int maxFrameSize; protected final int maxMessageSize; protected final int serverCompressionLevel; protected final int clientCompressionLevel; protected final boolean serverNoContextTakeover; protected final boolean clientNoContextTakeover; protected final int serverMaxWindowBits; protected final int clientMaxWindowBits; public WsEngineSettings(int maxFrameSize, int maxMessageSize, int serverCompressionLevel, int clientCompressionLevel, boolean serverNoContextTakeover, boolean clientNoContextTakeover, int serverMaxWindowBits, int clientMaxWindowBits) { this.maxFrameSize = maxFrameSize; this.maxMessageSize = maxMessageSize; this.serverCompressionLevel = serverCompressionLevel; this.clientCompressionLevel = clientCompressionLevel; this.serverNoContextTakeover = serverNoContextTakeover; this.clientNoContextTakeover = clientNoContextTakeover; this.serverMaxWindowBits = serverMaxWindowBits; this.clientMaxWindowBits = clientMaxWindowBits; } public final int maxFrameSize() { return this.maxFrameSize; } public WsEngineSettings maxFrameSize(int maxFrameSize) { return copy(maxFrameSize, this.maxMessageSize, this.serverCompressionLevel, this.clientCompressionLevel, this.serverNoContextTakeover, this.clientNoContextTakeover, this.serverMaxWindowBits, this.clientMaxWindowBits); } public final int maxMessageSize() { return this.maxMessageSize; } public WsEngineSettings maxMessageSize(int maxMessageSize) { return copy(this.maxFrameSize, maxMessageSize, this.serverCompressionLevel, this.clientCompressionLevel, this.serverNoContextTakeover, this.clientNoContextTakeover, this.serverMaxWindowBits, this.clientMaxWindowBits); } public final int serverCompressionLevel() { return this.serverCompressionLevel; } public WsEngineSettings serverCompressionLevel(int serverCompressionLevel) { return copy(this.maxFrameSize, this.maxMessageSize, serverCompressionLevel, this.clientCompressionLevel, this.serverNoContextTakeover, this.clientNoContextTakeover, this.serverMaxWindowBits, this.clientMaxWindowBits); } public final int clientCompressionLevel() { return this.clientCompressionLevel; } public WsEngineSettings clientCompressionLevel(int clientCompressionLevel) { return copy(this.maxFrameSize, this.maxMessageSize, this.serverCompressionLevel, clientCompressionLevel, this.serverNoContextTakeover, this.clientNoContextTakeover, this.serverMaxWindowBits, this.clientMaxWindowBits); } public WsEngineSettings compressionLevel(int serverCompressionLevel, int clientCompressionLevel) { return copy(this.maxFrameSize, this.maxMessageSize, serverCompressionLevel, clientCompressionLevel, this.serverNoContextTakeover, this.clientNoContextTakeover, this.serverMaxWindowBits, this.clientMaxWindowBits); } public final boolean serverNoContextTakeover() { return this.serverNoContextTakeover; } public WsEngineSettings serverNoContextTakeover(boolean serverNoContextTakeover) { return copy(this.maxFrameSize, this.maxMessageSize, this.serverCompressionLevel, this.clientCompressionLevel, serverNoContextTakeover, this.clientNoContextTakeover, this.serverMaxWindowBits, this.clientMaxWindowBits); } public final boolean clientNoContextTakeover() { return this.clientNoContextTakeover; } public WsEngineSettings clientNoContextTakeover(boolean clientNoContextTakeover) { return copy(this.maxFrameSize, this.maxMessageSize, this.serverCompressionLevel, this.clientCompressionLevel, this.serverNoContextTakeover, clientNoContextTakeover, this.serverMaxWindowBits, this.clientMaxWindowBits); } public final int serverMaxWindowBits() { return this.serverMaxWindowBits; } public WsEngineSettings serverMaxWindowBits(int serverMaxWindowBits) { return copy(this.maxFrameSize, this.maxMessageSize, this.serverCompressionLevel, this.clientCompressionLevel, this.serverNoContextTakeover, this.clientNoContextTakeover, serverMaxWindowBits, this.clientMaxWindowBits); } public final int clientMaxWindowBits() { return this.clientMaxWindowBits; } public WsEngineSettings clientMaxWindowBits(int clientMaxWindowBits) { return copy(this.maxFrameSize, this.maxMessageSize, this.serverCompressionLevel, this.clientCompressionLevel, this.serverNoContextTakeover, this.clientNoContextTakeover, this.serverMaxWindowBits, clientMaxWindowBits); } public FingerTrieSeq<WebSocketExtension> extensions() { if (serverCompressionLevel != 0 && clientCompressionLevel != 0) { final WebSocketExtension permessageDeflate = WebSocketExtension.permessageDeflate( this.serverNoContextTakeover, this.clientNoContextTakeover, this.serverMaxWindowBits, 0); return FingerTrieSeq.of(permessageDeflate); } return FingerTrieSeq.empty(); } public FingerTrieSeq<WebSocketExtension> acceptExtensions(FingerTrieSeq<WebSocketExtension> requestExtensions) { WebSocketExtension permessageDeflate = null; for (WebSocketExtension extension : requestExtensions) { if ("permessage-deflate".equals(extension.name()) && permessageDeflate == null && this.serverCompressionLevel != 0 && this.clientCompressionLevel != 0) { boolean requestServerNoContextTakeover = false; boolean requestClientNoContextTakeover = false; int requestServerMaxWindowBits = 15; int requestClientMaxWindowBits = 15; for (WebSocketParam param : extension.params()) { final String key = param.key(); final String value = param.value(); if ("server_no_context_takeover".equals(key)) { requestServerNoContextTakeover = true; } else if ("client_no_context_takeover".equals(key)) { requestClientNoContextTakeover = true; } else if ("server_max_window_bits".equals(key)) { try { requestServerMaxWindowBits = Integer.parseInt(value); } catch (NumberFormatException error) { throw new WsException("invalid permessage-deflate; " + param.toHttp()); } } else if ("client_max_window_bits".equals(key)) { if (value.isEmpty()) { requestClientMaxWindowBits = 0; } else { try { requestClientMaxWindowBits = Integer.parseInt(value); } catch (NumberFormatException error) { throw new WsException("invalid permessage-deflate; " + param.toHttp()); } } } else { throw new WsException("invalid permessage-deflate; " + param.toHttp()); } } if (requestClientMaxWindowBits != 0 && clientMaxWindowBits != 15) { continue; } else if (requestClientMaxWindowBits == 0) { requestClientMaxWindowBits = clientMaxWindowBits; } permessageDeflate = WebSocketExtension.permessageDeflate( requestServerNoContextTakeover || this.serverNoContextTakeover, requestClientNoContextTakeover || this.clientNoContextTakeover, Math.min(requestServerMaxWindowBits, this.serverMaxWindowBits), Math.min(requestClientMaxWindowBits, this.clientMaxWindowBits)); } } FingerTrieSeq<WebSocketExtension> responseExtensions = FingerTrieSeq.empty(); if (permessageDeflate != null) { responseExtensions = responseExtensions.appended(permessageDeflate); } return responseExtensions; } public WsRequest handshakeRequest(Uri uri, FingerTrieSeq<String> protocols, FingerTrieSeq<HttpHeader> headers) { return WsRequest.from(uri, protocols, extensions(), headers); } public WsRequest handshakeRequest(Uri uri, FingerTrieSeq<String> protocols, HttpHeader... headers) { return handshakeRequest(uri, protocols, FingerTrieSeq.of(headers)); } public WsRequest handshakeRequest(Uri uri, FingerTrieSeq<String> protocols) { return handshakeRequest(uri, protocols, FingerTrieSeq.<HttpHeader>empty()); } public WsRequest handshakeRequest(Uri uri, HttpHeader... headers) { return handshakeRequest(uri, FingerTrieSeq.<String>empty(), FingerTrieSeq.of(headers)); } public WsRequest handshakeRequest(Uri uri) { return handshakeRequest(uri, FingerTrieSeq.<String>empty(), FingerTrieSeq.<HttpHeader>empty()); } public Value toValue() { return engineForm().mold(this).toValue(); } protected WsEngineSettings copy(int maxFrameSize, int maxMessageSize, int serverCompressionLevel, int clientCompressionLevel, boolean serverNoContextTakeover, boolean clientNoContextTakeover, int serverMaxWindowBits, int clientMaxWindowBits) { return new WsEngineSettings(maxFrameSize, maxMessageSize, serverCompressionLevel, clientCompressionLevel, serverNoContextTakeover, clientNoContextTakeover, serverMaxWindowBits, clientMaxWindowBits); } public boolean canEqual(Object other) { return other instanceof WsEngineSettings; } @Override public boolean equals(Object other) { if (this == other) { return true; } else if (other instanceof WsEngineSettings) { final WsEngineSettings that = (WsEngineSettings) other; return that.canEqual(this) && this.maxFrameSize == that.maxFrameSize && this.maxMessageSize == that.maxMessageSize && this.serverCompressionLevel == that.serverCompressionLevel && this.clientCompressionLevel == that.clientCompressionLevel && this.serverNoContextTakeover == that.serverNoContextTakeover && this.clientNoContextTakeover == that.clientNoContextTakeover && this.serverMaxWindowBits == that.serverMaxWindowBits && this.clientMaxWindowBits == that.clientMaxWindowBits; } return false; } @Override public int hashCode() { if (hashSeed == 0) { hashSeed = Murmur3.seed(WsEngineSettings.class); } return Murmur3.mash(Murmur3.mix(Murmur3.mix(Murmur3.mix(Murmur3.mix(Murmur3.mix( Murmur3.mix(Murmur3.mix(Murmur3.mix(hashSeed, this.maxFrameSize), this.maxMessageSize), this.serverCompressionLevel), this.clientCompressionLevel), Murmur3.hash(this.serverNoContextTakeover)), Murmur3.hash(this.clientNoContextTakeover)), this.serverMaxWindowBits), this.clientMaxWindowBits)); } @Override public void debug(Output<?> output) { output = output.write("WsEngineSettings").write('.').write("standard").write('(').write(')') .write('.').write("maxFrameSize").write('(').debug(this.maxFrameSize).write(')') .write('.').write("maxMessageSize").write('(').debug(this.maxMessageSize).write(')') .write('.').write("serverCompressionLevel").write('(').debug(this.serverCompressionLevel).write(')') .write('.').write("clientCompressionLevel").write('(').debug(this.clientCompressionLevel).write(')') .write('.').write("serverNoContextTakeover").write('(').debug(this.serverNoContextTakeover).write(')') .write('.').write("clientNoContextTakeover").write('(').debug(this.clientNoContextTakeover).write(')') .write('.').write("serverMaxWindowBits").write('(').debug(this.serverMaxWindowBits).write(')') .write('.').write("clientMaxWindowBits").write('(').debug(this.clientMaxWindowBits).write(')'); } @Override public String toString() { return Format.debug(this); } private static int hashSeed; private static WsEngineSettings standard; private static Form<WsEngineSettings> engineForm; public static WsEngineSettings standard() { if (standard == null) { int maxFrameSize; try { maxFrameSize = Integer.parseInt(System.getProperty("swim.ws.max.frame.size")); } catch (NumberFormatException error) { maxFrameSize = 16 * 1024 * 1024; } int maxMessageSize; try { maxMessageSize = Integer.parseInt(System.getProperty("swim.ws.max.message.size")); } catch (NumberFormatException error) { maxMessageSize = 16 * 1024 * 1024; } int serverCompressionLevel; try { serverCompressionLevel = Integer.parseInt(System.getProperty("swim.ws.server.compression.level")); } catch (NumberFormatException error) { serverCompressionLevel = 0; } int clientCompressionLevel; try { clientCompressionLevel = Integer.parseInt(System.getProperty("swim.ws.client.compression.level")); } catch (NumberFormatException error) { clientCompressionLevel = 0; } final boolean serverNoContextTakeover = Boolean.parseBoolean(System.getProperty("swim.ws.server.no.context.takeover")); final boolean clientNoContextTakeover = Boolean.parseBoolean(System.getProperty("swim.ws.client.no.context.takeover")); int serverMaxWindowBits; try { serverMaxWindowBits = Integer.parseInt(System.getProperty("swim.ws.server.max.window.bits")); } catch (NumberFormatException error) { serverMaxWindowBits = 15; } int clientMaxWindowBits; try { clientMaxWindowBits = Integer.parseInt(System.getProperty("swim.ws.client.max.window.bits")); } catch (NumberFormatException error) { clientMaxWindowBits = 15; } standard = new WsEngineSettings(maxFrameSize, maxMessageSize, serverCompressionLevel, clientCompressionLevel, serverNoContextTakeover, clientNoContextTakeover, serverMaxWindowBits, clientMaxWindowBits); } return standard; } public static WsEngineSettings noCompression() { return standard().compressionLevel(0, 0); } public static WsEngineSettings defaultCompression() { return standard().compressionLevel(-1, -1); } public static WsEngineSettings fastestCompression() { return standard().compressionLevel(1, 1); } public static WsEngineSettings bestCompression() { return standard().compressionLevel(9, 9); } @Kind public static Form<WsEngineSettings> engineForm() { if (engineForm == null) { engineForm = new WsEngineSettingsForm(); } return engineForm; } } final class WsEngineSettingsForm extends Form<WsEngineSettings> { @Override public WsEngineSettings unit() { return WsEngineSettings.standard(); } @Override public Class<?> type() { return WsEngineSettings.class; } @Override public Item mold(WsEngineSettings settings) { if (settings != null) { final WsEngineSettings standard = WsEngineSettings.standard(); final Record record = Record.create(8); if (settings.maxFrameSize != standard.maxFrameSize) { record.slot("maxFrameSize", settings.maxFrameSize); } if (settings.maxMessageSize != standard.maxMessageSize) { record.slot("maxMessageSize", settings.maxMessageSize); } if (settings.serverCompressionLevel != standard.serverCompressionLevel) { record.slot("serverCompressionLevel", settings.serverCompressionLevel); } if (settings.clientCompressionLevel != standard.clientCompressionLevel) { record.slot("clientCompressionLevel", settings.clientCompressionLevel); } if (settings.serverNoContextTakeover != standard.serverNoContextTakeover) { record.slot("serverNoContextTakeover", settings.serverNoContextTakeover); } if (settings.clientNoContextTakeover != standard.clientNoContextTakeover) { record.slot("clientNoContextTakeover", settings.clientNoContextTakeover); } if (settings.serverMaxWindowBits != standard.serverMaxWindowBits) { record.slot("serverMaxWindowBits", settings.serverMaxWindowBits); } if (settings.clientMaxWindowBits != standard.clientMaxWindowBits) { record.slot("clientMaxWindowBits", settings.clientMaxWindowBits); } return record; } else { return Item.extant(); } } @Override public WsEngineSettings cast(Item item) { final Value value = item.toValue(); final WsEngineSettings standard = WsEngineSettings.standard(); final int maxFrameSize = value.get("maxFrameSize").intValue(standard.maxFrameSize); final int maxMessageSize = value.get("maxMessageSize").intValue(standard.maxMessageSize); final int serverCompressionLevel = value.get("serverCompressionLevel").intValue(standard.serverCompressionLevel); final int clientCompressionLevel = value.get("clientCompressionLevel").intValue(standard.clientCompressionLevel); final boolean serverNoContextTakeover = value.get("serverNoContextTakeover").booleanValue(standard.serverNoContextTakeover); final boolean clientNoContextTakeover = value.get("clientNoContextTakeover").booleanValue(standard.clientNoContextTakeover); final int serverMaxWindowBits = value.get("serverMaxWindowBits").intValue(standard.serverMaxWindowBits); final int clientMaxWindowBits = value.get("clientMaxWindowBits").intValue(standard.clientMaxWindowBits); return new WsEngineSettings(maxFrameSize, maxMessageSize, serverCompressionLevel, clientCompressionLevel, serverNoContextTakeover, clientNoContextTakeover, serverMaxWindowBits, clientMaxWindowBits); } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsException.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; public class WsException extends RuntimeException { private static final long serialVersionUID = 1L; public WsException(String message, Throwable cause) { super(message, cause); } public WsException(String message) { super(message); } public WsException(Throwable cause) { super(cause); } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsFragment.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import swim.codec.Debug; import swim.codec.Decoder; import swim.codec.Encoder; import swim.codec.Format; import swim.codec.Output; import swim.codec.OutputBuffer; public final class WsFragment<T> extends WsFrame<T> implements Debug { final WsOpcode opcode; final Decoder<T> content; WsFragment(WsOpcode opcode, Decoder<T> content) { this.opcode = opcode; this.content = content; } @Override public boolean isDefined() { return false; } @Override public T get() { return this.content.bind(); } @Override public WsOpcode opcode() { return this.opcode; } @Override public Object payload() { return this.content.bind(); } @Override public Encoder<?, ?> contentEncoder(WsEncoder ws) { throw new UnsupportedOperationException(); } @Override public Encoder<?, ?> encodeContent(OutputBuffer<?> output, WsEncoder ws) { throw new UnsupportedOperationException(); } public Decoder<T> contentDecoder() { return this.content; } @Override public void debug(Output<?> output) { output = output.write("WsFragment").write('.').write("from").write('(') .debug(this.opcode).write(", ").debug(this.content).write(')'); } @Override public String toString() { return Format.debug(this); } public static <T> WsFragment<T> from(WsOpcode opcode, Decoder<T> content) { return new WsFragment<T>(opcode, content); } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsFrame.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import swim.codec.Encoder; import swim.codec.OutputBuffer; public abstract class WsFrame<T> { WsFrame() { // stub } public abstract boolean isDefined(); public abstract T get(); public abstract WsOpcode opcode(); public abstract Object payload(); public abstract Encoder<?, ?> contentEncoder(WsEncoder ws); public abstract Encoder<?, ?> encodeContent(OutputBuffer<?> output, WsEncoder ws); }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsFrameDecoder.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import swim.codec.Decoder; import swim.codec.DecoderException; import swim.codec.InputBuffer; final class WsFrameDecoder<O> extends Decoder<WsFrame<O>> { final WsDecoder ws; final Decoder<O> content; final int finRsvOp; final long position; final long offset; final long length; final byte[] maskingKey; final int step; WsFrameDecoder(WsDecoder ws, Decoder<O> content, int finRsvOp, long position, long offset, long length, byte[] maskingKey, int step) { this.ws = ws; this.position = position; this.content = content; this.finRsvOp = finRsvOp; this.offset = offset; this.length = length; this.maskingKey = maskingKey; this.step = step; } WsFrameDecoder(WsDecoder ws, Decoder<O> content) { this(ws, content, 0, 0L, 0L, 0L, null, 1); } @Override public Decoder<WsFrame<O>> feed(InputBuffer input) { return decode(input, this.ws, this.content, this.finRsvOp, this.position, this.offset, this.length, this.maskingKey, this.step); } static <O> Decoder<WsFrame<O>> decode(InputBuffer input, WsDecoder ws, Decoder<O> content, int finRsvOp, long position, long offset, long length, byte[] maskingKey, int step) { if (step == 1 && input.isCont()) { // decode finRsvOp finRsvOp = input.head(); input = input.step(); step = 2; } if (step == 2 && input.isCont()) { // decode maskLength final int maskLength = input.head(); input = input.step(); if ((maskLength & 0x80) != 0) { maskingKey = new byte[4]; } final int len = maskLength & 0x7f; if (len == 126) { // short length step = 3; } else if (len == 127) { // long length step = 5; } else { length = (long) len; step = maskingKey != null ? 13 : 17; } } if (step >= 3 && step <= 4) { // decode short length while (input.isCont()) { length = (length << 8) | (long) input.head(); input = input.step(); if (step < 4) { step += 1; } else { step = maskingKey != null ? 13 : 17; break; } } } if (step >= 5 && step <= 12) { // decode long length while (input.isCont()) { length = (length << 8) | (long) input.head(); input = input.step(); if (step < 12) { step += 1; } else { step = maskingKey != null ? 13 : 17; break; } } } if (step >= 13 && step <= 16) { // decode masking key while (input.isCont()) { maskingKey[step - 13] = (byte) input.head(); input = input.step(); if (step < 16) { step += 1; } else { step = 17; break; } } } if (step == 17) { // decode payload final int base = input.index(); final int size = (int) Math.min(length - offset, input.remaining()); if (maskingKey != null) { for (int i = 0; i < size; i += 1) { input.set(base + i, (input.get(base + i) ^ maskingKey[(int) (position + i) & 0x3]) & 0xff); } } position += size; offset += size; final boolean eof = offset == length && (finRsvOp & 0x80) != 0; final boolean inputPart = input.isPart(); input = input.isPart(!eof); if (input.remaining() < size) { content = content.feed(input); } else { final int inputLimit = input.limit(); input = input.limit(base + size); content = content.feed(input); input = input.limit(inputLimit); } input = input.isPart(inputPart); if (input.index() != base + size) { return error(new DecoderException("undecoded websocket data")); } else if (content.isError()) { return content.asError(); } else if (content.isDone()) { if (offset == length) { if ((finRsvOp & 0x80) != 0) { final int opcode = finRsvOp & 0xf; if (opcode < 0x8) { // decoded message return done(ws.message(content.bind())); } else { // decoded control frame return done(ws.control(WsOpcode.from(opcode), content.bind())); } } else { return error(new DecoderException("decoded unfinished websocket message")); } } else { return error(new DecoderException("decoded incomplete websocket frame")); } } else if (offset == length) { if ((finRsvOp & 0x80) == 0) { final int opcode = finRsvOp & 0xf; if (opcode < 0x8) { // decoded fragment return done(ws.fragment(WsOpcode.from(opcode), content)); } else { return error(new DecoderException("decoded fragmented control frame")); } } else { return error(new DecoderException("undecoded websocket message")); } } } if (input.isDone()) { return error(new DecoderException("incomplete")); } else if (input.isError()) { return error(input.trap()); } return new WsFrameDecoder<O>(ws, content, finRsvOp, position, offset, length, maskingKey, step); } static <O> Decoder<WsFrame<O>> decode(InputBuffer input, WsDecoder ws, Decoder<O> content) { return decode(input, ws, content, 0, 0L, 0L, 0L, null, 1); } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsFrameDeflater.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import swim.codec.Encoder; import swim.codec.EncoderException; import swim.codec.OutputBuffer; import swim.deflate.Deflate; import swim.deflate.DeflateException; final class WsFrameDeflater<O> extends Encoder<Object, WsFrame<O>> { final WsDeflateEncoder ws; final WsFrame<O> frame; final Encoder<?, ?> content; final long position; final long offset; WsFrameDeflater(WsDeflateEncoder ws, WsFrame<O> frame, Encoder<?, ?> content, long position, long offset) { this.ws = ws; this.frame = frame; this.content = content; this.position = position; this.offset = offset; } WsFrameDeflater(WsDeflateEncoder ws, WsFrame<O> frame) { this(ws, frame, null, 0L, 0L); } @Override public Encoder<Object, WsFrame<O>> pull(OutputBuffer<?> output) { return encode(output, this.ws, this.frame, this.content, this.position, this.offset); } @SuppressWarnings("unchecked") static <O> Encoder<Object, WsFrame<O>> encode(OutputBuffer<?> output, WsDeflateEncoder ws, WsFrame<O> frame, Encoder<?, ?> content, long position, long offset) { final boolean isMasked = ws.isMasked(); final int outputSize = output.remaining(); final int maskSize = isMasked ? 4 : 0; final int maxHeaderSize = (outputSize <= 127 ? 2 : outputSize <= 65539 ? 4 : 10) + maskSize; if (outputSize >= maxHeaderSize) { // prepare output buffer for payload final int outputBase = output.index(); final int maxPayloadBase = outputBase + maxHeaderSize; if (content == null) { ((Deflate<Object>) ws.deflate).input = (Encoder<?, Object>) frame.contentEncoder(ws); } else { ((Deflate<Object>) ws.deflate).input = (Encoder<?, Object>) content; } ws.deflate.next_out = output.array(); ws.deflate.next_out_index = output.arrayOffset() + maxPayloadBase; ws.deflate.avail_out = outputSize - maxHeaderSize; try { // deflate payload final boolean needsMore = ws.deflate.deflate(ws.flush); content = ws.deflate.input; final boolean eof = content.isDone() && !needsMore; final int payloadSize = ws.deflate.next_out_index - (output.arrayOffset() + maxPayloadBase) - (eof ? 4 : 0); final int headerSize = (payloadSize <= 125 ? 2 : payloadSize <= 65535 ? 4 : 10) + maskSize; // encode header final WsOpcode opcode = frame.opcode(); final int finRsvOp; if (eof) { if (offset == 0L) { finRsvOp = 0xc0 | opcode.code; } else { finRsvOp = 0x80; } } else if (content.isError()) { return content.asError(); } else if (offset == 0L) { finRsvOp = 0x40 | opcode.code; } else { finRsvOp = 0x00; } output = output.index(outputBase); output = output.write(finRsvOp); if (payloadSize < 126) { output = output.write(isMasked ? 0x80 | payloadSize : payloadSize); } else if (payloadSize < 1 << 16) { output = output.write(isMasked ? 254 : 126) .write(payloadSize >>> 8) .write(payloadSize); } else { output = output.write(isMasked ? 255 : 127) .write(0) .write(0) .write(0) .write(0) .write(payloadSize >>> 24) .write(payloadSize >>> 16) .write(payloadSize >>> 8) .write(payloadSize); } if (isMasked) { // generate and encode masking key final byte[] maskingKey = new byte[4]; ws.maskingKey(maskingKey); output = output.write(maskingKey[0] & 0xff) .write(maskingKey[1] & 0xff) .write(maskingKey[2] & 0xff) .write(maskingKey[3] & 0xff); // mask payload, shifting if header smaller than anticipated for (int i = 0; i < payloadSize; i += 1) { output.set(outputBase + headerSize + i, (output.get(outputBase + maxHeaderSize + i) ^ maskingKey[(int) (position + i) & 0x3]) & 0xff); } } else if (headerSize < maxHeaderSize) { // shift payload if header smaller than anticipated output = output.move(maxHeaderSize, headerSize, payloadSize); } position += payloadSize; offset += payloadSize; output = output.index(outputBase + headerSize + payloadSize); if (eof) { return done(frame); } } catch (DeflateException cause) { return error(new EncoderException(cause)); } finally { ws.deflate.input = null; ws.deflate.next_out = null; ws.deflate.next_out_index = 0; ws.deflate.avail_out = 0; } } if (output.isDone()) { return error(new EncoderException("truncated")); } else if (output.isError()) { return error(output.trap()); } return new WsFrameDeflater<O>(ws, frame, content, position, offset); } static <O> Encoder<Object, WsFrame<O>> encode(OutputBuffer<?> output, WsDeflateEncoder ws, WsFrame<O> frame) { return encode(output, ws, frame, null, 0L, 0L); } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsFrameEncoder.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import swim.codec.Encoder; import swim.codec.EncoderException; import swim.codec.OutputBuffer; final class WsFrameEncoder<O> extends Encoder<Object, WsFrame<O>> { final WsEncoder ws; final WsFrame<O> frame; final Encoder<?, ?> content; final long position; final long offset; WsFrameEncoder(WsEncoder ws, WsFrame<O> frame, Encoder<?, ?> content, long position, long offset) { this.ws = ws; this.frame = frame; this.content = content; this.position = position; this.offset = offset; } WsFrameEncoder(WsEncoder ws, WsFrame<O> frame) { this(ws, frame, null, 0L, 0L); } @Override public Encoder<Object, WsFrame<O>> pull(OutputBuffer<?> output) { return encode(output, this.ws, this.frame, this.content, this.position, this.offset); } static <O> Encoder<Object, WsFrame<O>> encode(OutputBuffer<?> output, WsEncoder ws, WsFrame<O> frame, Encoder<?, ?> content, long position, long offset) { final boolean isMasked = ws.isMasked(); final int outputSize = output.remaining(); final int maskSize = isMasked ? 4 : 0; final int maxHeaderSize = (outputSize <= 127 ? 2 : outputSize <= 65539 ? 4 : 10) + maskSize; if (outputSize >= maxHeaderSize) { // prepare output buffer for payload final int outputBase = output.index(); final int maxPayloadBase = outputBase + maxHeaderSize; output = output.index(maxPayloadBase); // encode payload final Encoder<?, ?> nextContent; if (content == null) { nextContent = frame.encodeContent(output, ws); } else { nextContent = content.pull(output); } final int payloadSize = output.index() - maxPayloadBase; final int headerSize = (payloadSize <= 125 ? 2 : payloadSize <= 65535 ? 4 : 10) + maskSize; // encode header final WsOpcode opcode = frame.opcode(); final int finRsvOp; if (nextContent.isDone()) { if (offset == 0L) { finRsvOp = 0x80 | opcode.code; } else { finRsvOp = 0x80; } } else if (nextContent.isError()) { return nextContent.asError(); } else if (offset == 0L) { finRsvOp = opcode.code; } else { finRsvOp = 0x00; } output = output.index(outputBase); if (!opcode.isControl() || (finRsvOp & 0x80) != 0) { // not a fragmented control frame content = nextContent; output = output.write(finRsvOp); if (payloadSize < 126) { output = output.write(isMasked ? 0x80 | payloadSize : payloadSize); } else if (payloadSize < 1 << 16) { output = output.write(isMasked ? 254 : 126) .write(payloadSize >>> 8) .write(payloadSize); } else { output = output.write(isMasked ? 255 : 127) .write(0) .write(0) .write(0) .write(0) .write(payloadSize >>> 24) .write(payloadSize >>> 16) .write(payloadSize >>> 8) .write(payloadSize); } if (isMasked) { // generate and encode masking key final byte[] maskingKey = new byte[4]; ws.maskingKey(maskingKey); output = output.write(maskingKey[0] & 0xff) .write(maskingKey[1] & 0xff) .write(maskingKey[2] & 0xff) .write(maskingKey[3] & 0xff); // mask payload, shifting if header smaller than anticipated for (int i = 0; i < payloadSize; i += 1) { output.set(outputBase + headerSize + i, (output.get(outputBase + maxHeaderSize + i) ^ maskingKey[(int) (position + i) & 0x3]) & 0xff); } } else if (headerSize < maxHeaderSize) { // shift payload if header smaller than anticipated output = output.move(maxHeaderSize, headerSize, payloadSize); } position += payloadSize; offset += payloadSize; output = output.index(outputBase + headerSize + payloadSize); if (content.isDone()) { return done(frame); } } } if (output.isDone()) { return error(new EncoderException("truncated")); } else if (output.isError()) { return error(output.trap()); } return new WsFrameEncoder<O>(ws, frame, content, position, offset); } static <O> Encoder<Object, WsFrame<O>> encode(OutputBuffer<?> output, WsEncoder ws, WsFrame<O> frame) { return encode(output, ws, frame, null, 0L, 0L); } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsFrameInflater.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import swim.codec.Decoder; import swim.codec.DecoderException; import swim.codec.InputBuffer; import swim.deflate.DeflateException; import swim.deflate.Inflate; final class WsFrameInflater<O> extends Decoder<WsFrame<O>> { final WsDeflateDecoder ws; final Decoder<O> content; final int finRsvOp; final long position; final long offset; final long length; final byte[] maskingKey; final int step; WsFrameInflater(WsDeflateDecoder ws, Decoder<O> content, int finRsvOp, long position, long offset, long length, byte[] maskingKey, int step) { this.ws = ws; this.content = content; this.finRsvOp = finRsvOp; this.position = position; this.offset = offset; this.length = length; this.maskingKey = maskingKey; this.step = step; } WsFrameInflater(WsDeflateDecoder ws, Decoder<O> content) { this(ws, content, 0, 0L, 0L, 0L, null, 1); } @Override public Decoder<WsFrame<O>> feed(InputBuffer input) { return decode(input, this.ws, this.content, this.finRsvOp, this.position, this.offset, this.length, this.maskingKey, this.step); } @SuppressWarnings("unchecked") static <O> Decoder<WsFrame<O>> decode(InputBuffer input, WsDeflateDecoder ws, Decoder<O> content, int finRsvOp, long position, long offset, long length, byte[] maskingKey, int step) { if (step == 1 && input.isCont()) { // decode finRsvOp finRsvOp = input.head(); input = input.step(); step = 2; } if (step == 2 && input.isCont()) { // decode maskLength final int maskLength = input.head(); input = input.step(); if ((maskLength & 0x80) != 0) { maskingKey = new byte[4]; } final int len = maskLength & 0x7f; if (len == 126) { // short length step = 3; } else if (len == 127) { // long length step = 5; } else { length = (long) len; step = maskingKey != null ? 13 : 17; } } if (step >= 3 && step <= 4) { // decode short length while (input.isCont()) { length = (length << 8) | (long) input.head(); input = input.step(); if (step < 4) { step += 1; } else { step = maskingKey != null ? 13 : 17; break; } } } if (step >= 5 && step <= 12) { // decode long length while (input.isCont()) { length = (length << 8) | (long) input.head(); input = input.step(); if (step < 12) { step += 1; } else { step = maskingKey != null ? 13 : 17; break; } } } if (step >= 13 && step <= 16) { // decode masking key while (input.isCont()) { maskingKey[step - 13] = (byte) input.head(); input = input.step(); if (step < 16) { step += 1; } else { step = 17; break; } } } if (step == 17) { // decode payload final int base = input.index(); final int size = (int) Math.min(length - offset, input.remaining()); if (maskingKey != null) { for (int i = 0; i < size; i += 1) { input.set(base + i, (input.get(base + i) ^ maskingKey[(int) (position + i) & 0x3]) & 0xff); } } position += size; offset += size; final boolean eof = offset == length && (finRsvOp & 0x80) != 0; ws.inflate.initWindow(); ws.inflate.next_out = ws.inflate.window; ((Inflate<Object>) ws.inflate).output = (Decoder<Object>) (Decoder<?>) content; ws.inflate.is_last = false; ws.inflate.next_in = input.array(); ws.inflate.next_in_index = input.arrayOffset() + base; ws.inflate.avail_in = Math.min(input.remaining(), size); try { boolean needsMore; do { ws.inflate.next_out_index = ws.inflate.wnext; ws.inflate.avail_out = ws.inflate.window.length - ws.inflate.wnext; needsMore = ws.inflate.inflate(Inflate.Z_SYNC_FLUSH); content = (Decoder<O>) ws.inflate.output; } while (needsMore && ws.inflate.avail_in > 0 && content.isCont()); input = input.index(ws.inflate.next_in_index - input.arrayOffset()); if (eof) { ws.inflate.next_in = EMPTY_BLOCK; ws.inflate.next_in_index = 0; ws.inflate.avail_in = 4; do { ws.inflate.next_out_index = ws.inflate.wnext; ws.inflate.avail_out = ws.inflate.window.length - ws.inflate.wnext; needsMore = ws.inflate.inflate(Inflate.Z_SYNC_FLUSH); content = (Decoder<O>) ws.inflate.output; } while (needsMore && ws.inflate.avail_in > 0 && content.isCont()); if (content.isCont()) { ws.inflate.window_buffer.index(ws.inflate.next_out_index).limit(ws.inflate.next_out_index).isPart(false); content = content.feed(ws.inflate.window_buffer); ((Inflate<Object>) ws.inflate).output = (Decoder<Object>) (Decoder<?>) content; } } } catch (DeflateException cause) { return error(cause); } finally { ws.inflate.next_out = null; ws.inflate.next_out_index = 0; ws.inflate.avail_out = 0; ws.inflate.next_in = null; ws.inflate.next_in_index = 0; ws.inflate.avail_in = 0; } if (input.index() != base + size) { return error(new DecoderException("undecoded websocket data")); } else if (content.isError()) { return content.asError(); } else if (content.isDone()) { if (offset == length) { if ((finRsvOp & 0x80) != 0) { final int opcode = finRsvOp & 0xf; if (opcode < 0x8) { // decoded message return done(ws.message(content.bind())); } else { // decoded control frame return done(ws.control(WsOpcode.from(opcode), content.bind())); } } else { return error(new DecoderException("decoded unfinished websocket message")); } } else { return error(new DecoderException("decoded incomplete websocket frame")); } } else if (offset == length) { if ((finRsvOp & 0x80) == 0) { final int opcode = finRsvOp & 0xf; if (opcode < 0x8) { // decoded fragment return done(ws.fragment(WsOpcode.from(opcode), content)); } else { return error(new DecoderException("decoded fragmented control frame")); } } else { return error(new DecoderException("undecoded websocket message")); } } } if (input.isDone()) { return error(new DecoderException("incomplete")); } else if (input.isError()) { return error(input.trap()); } return new WsFrameInflater<O>(ws, content, finRsvOp, position, offset, length, maskingKey, step); } static <O> Decoder<WsFrame<O>> decode(InputBuffer input, WsDeflateDecoder ws, Decoder<O> content) { return decode(input, ws, content, 0, 0L, 0L, 0L, null, 1); } private static final byte[] EMPTY_BLOCK = {(byte) 0x00, (byte) 0x00, (byte) 0xff, (byte) 0xff}; }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsOpcode.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import swim.codec.Debug; import swim.codec.Output; public enum WsOpcode implements Debug { INVALID(-1), CONTINUATION(0x0), TEXT(0x1), BINARY(0x2), RESERVED_3(0x3), RESERVED_4(0x4), RESERVED_5(0x5), RESERVED_6(0x6), RESERVED_7(0x7), CLOSE(0x8), PING(0x9), PONG(0xa), RESERVED_B(0xb), RESERVED_C(0xc), RESERVED_D(0xd), RESERVED_E(0xe), RESERVED_F(0xf); public final int code; WsOpcode(int code) { this.code = code; } public boolean isValid() { return this.code >= 0; } public boolean isData() { return this.code >= 0x1 && this.code <= 0x2; } public boolean isControl() { return this.code >= 0x8; } public boolean isReserved() { return this.code >= 0x3 && this.code <= 0x7 || this.code >= 0xb && this.code <= 0xf; } public boolean isContinuation() { return this.code == 0x0; } public boolean isText() { return this.code == 0x1; } public boolean isBinary() { return this.code == 0x2; } public boolean isClose() { return this.code == 0x8; } public boolean isPing() { return this.code == 0x9; } public boolean isPong() { return this.code == 0xa; } @Override public void debug(Output<?> output) { output = output.write("WsOpcode").write('.').write(name()); } public static WsOpcode from(int code) { switch (code) { case 0x0: return CONTINUATION; case 0x1: return TEXT; case 0x2: return BINARY; case 0x3: return RESERVED_3; case 0x4: return RESERVED_4; case 0x5: return RESERVED_5; case 0x6: return RESERVED_6; case 0x7: return RESERVED_7; case 0x8: return CLOSE; case 0x9: return PING; case 0xa: return PONG; case 0xb: return RESERVED_B; case 0xc: return RESERVED_C; case 0xd: return RESERVED_D; case 0xe: return RESERVED_E; case 0xf: return RESERVED_F; default: return INVALID; } } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsOpcodeDecoder.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import swim.codec.Decoder; import swim.codec.DecoderException; import swim.codec.InputBuffer; final class WsOpcodeDecoder<O> extends Decoder<WsFrame<O>> { final WsDecoder ws; final Decoder<O> content; WsOpcodeDecoder(WsDecoder ws, Decoder<O> content) { this.ws = ws; this.content = content; } @Override public Decoder<WsFrame<O>> feed(InputBuffer input) { return decode(input, this.ws, this.content); } static <O> Decoder<WsFrame<O>> decode(InputBuffer input, WsDecoder ws, Decoder<O> content) { if (input.isCont()) { final int finRsvOp = input.head(); return ws.decodeFrame(finRsvOp, content, input); } else if (input.isDone()) { return error(new DecoderException("incomplete")); } else if (input.isError()) { return error(input.trap()); } return new WsOpcodeDecoder<O>(ws, content); } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsPing.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import java.nio.ByteBuffer; import swim.codec.Binary; import swim.codec.Debug; import swim.codec.Encoder; import swim.codec.Format; import swim.codec.Output; import swim.codec.OutputBuffer; import swim.structure.Data; import swim.util.Murmur3; public final class WsPing<P, T> extends WsControl<P, T> implements Debug { final P payload; final Encoder<?, ?> content; WsPing(P payload, Encoder<?, ?> content) { this.payload = payload; this.content = content; } @Override public WsOpcode opcode() { return WsOpcode.PING; } @Override public P payload() { return this.payload; } @Override public Encoder<?, ?> contentEncoder(WsEncoder ws) { return this.content; } @Override public Encoder<?, ?> encodeContent(OutputBuffer<?> output, WsEncoder ws) { return this.content.pull(output); } @Override public boolean equals(Object other) { if (this == other) { return true; } else if (other instanceof WsPing<?, ?>) { final WsPing<?, ?> that = (WsPing<?, ?>) other; return (this.payload == null ? that.payload == null : this.payload.equals(that.payload)); } return false; } @Override public int hashCode() { if (hashSeed == 0) { hashSeed = Murmur3.seed(WsPing.class); } return Murmur3.mash(Murmur3.mix(hashSeed, Murmur3.hash(this.payload))); } @Override public void debug(Output<?> output) { output = output.write("WsPing").write('.').write("from").write('(') .debug(this.payload).write(", ").debug(this.content).write(')'); } @Override public String toString() { return Format.debug(this); } private static int hashSeed; public static <P, T> WsPing<P, T> empty() { return new WsPing<P, T>(null, Encoder.done()); } public static <P, T> WsPing<P, T> from(P payload, Encoder<?, ?> content) { return new WsPing<P, T>(payload, content); } @SuppressWarnings("unchecked") public static <P, T> WsPing<P, T> from(P payload) { if (payload instanceof Data) { return (WsPing<P, T>) from((Data) payload); } else { return new WsPing<P, T>(payload, Encoder.done()); } } public static <T> WsPing<ByteBuffer, T> from(ByteBuffer payload) { return new WsPing<ByteBuffer, T>(payload.duplicate(), Binary.byteBufferWriter(payload)); } public static <T> WsPing<Data, T> from(Data payload) { return new WsPing<Data, T>(payload, payload.writer()); } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsPong.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import java.nio.ByteBuffer; import swim.codec.Binary; import swim.codec.Debug; import swim.codec.Encoder; import swim.codec.Format; import swim.codec.Output; import swim.codec.OutputBuffer; import swim.structure.Data; import swim.util.Murmur3; public final class WsPong<P, T> extends WsControl<P, T> implements Debug { final P payload; final Encoder<?, ?> content; WsPong(P payload, Encoder<?, ?> content) { this.payload = payload; this.content = content; } @Override public WsOpcode opcode() { return WsOpcode.PONG; } @Override public P payload() { return this.payload; } @Override public Encoder<?, ?> contentEncoder(WsEncoder ws) { return this.content; } @Override public Encoder<?, ?> encodeContent(OutputBuffer<?> output, WsEncoder ws) { return this.content.pull(output); } @Override public boolean equals(Object other) { if (this == other) { return true; } else if (other instanceof WsPong<?, ?>) { final WsPong<?, ?> that = (WsPong<?, ?>) other; return (this.payload == null ? that.payload == null : this.payload.equals(that.payload)); } return false; } @Override public int hashCode() { if (hashSeed == 0) { hashSeed = Murmur3.seed(WsPong.class); } return Murmur3.mash(Murmur3.mix(hashSeed, Murmur3.hash(this.payload))); } @Override public void debug(Output<?> output) { output = output.write("WsPong").write('.').write("from").write('(') .debug(this.payload).write(", ").debug(this.content).write(')'); } @Override public String toString() { return Format.debug(this); } private static int hashSeed; public static <P, T> WsPong<P, T> empty() { return new WsPong<P, T>(null, Encoder.done()); } public static <P, T> WsPong<P, T> from(P payload, Encoder<?, ?> content) { return new WsPong<P, T>(payload, content); } @SuppressWarnings("unchecked") public static <P, T> WsPong<P, T> from(P payload) { if (payload instanceof Data) { return (WsPong<P, T>) from((Data) payload); } else { return new WsPong<P, T>(payload, Encoder.done()); } } public static <T> WsPong<ByteBuffer, T> from(ByteBuffer payload) { return new WsPong<ByteBuffer, T>(payload.duplicate(), Binary.byteBufferWriter(payload)); } public static <T> WsPong<Data, T> from(Data payload) { return new WsPong<Data, T>(payload, payload.writer()); } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsRequest.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import swim.collections.FingerTrieSeq; import swim.http.HttpHeader; import swim.http.HttpRequest; import swim.http.HttpResponse; import swim.http.HttpStatus; import swim.http.UpgradeProtocol; import swim.http.WebSocketExtension; import swim.http.header.Connection; import swim.http.header.Host; import swim.http.header.SecWebSocketAccept; import swim.http.header.SecWebSocketExtensions; import swim.http.header.SecWebSocketKey; import swim.http.header.SecWebSocketProtocol; import swim.http.header.SecWebSocketVersion; import swim.http.header.Upgrade; import swim.uri.Uri; import swim.uri.UriPath; import swim.util.Builder; /** * WebSocket handshake request. */ public class WsRequest { protected final HttpRequest<?> httpRequest; protected final SecWebSocketKey key; protected final FingerTrieSeq<String> protocols; protected final FingerTrieSeq<WebSocketExtension> extensions; public WsRequest(HttpRequest<?> httpRequest, SecWebSocketKey key, FingerTrieSeq<String> protocols, FingerTrieSeq<WebSocketExtension> extensions) { this.httpRequest = httpRequest; this.key = key; this.protocols = protocols; this.extensions = extensions; } public final HttpRequest<?> httpRequest() { return this.httpRequest; } public final SecWebSocketKey key() { return this.key; } public final FingerTrieSeq<String> protocols() { return this.protocols; } public final FingerTrieSeq<WebSocketExtension> extensions() { return this.extensions; } public HttpResponse<?> httpResponse(String protocol, FingerTrieSeq<WebSocketExtension> extensions, FingerTrieSeq<HttpHeader> headers) { final Builder<HttpHeader, FingerTrieSeq<HttpHeader>> responseHeaders = FingerTrieSeq.builder(); responseHeaders.add(Connection.upgrade()); responseHeaders.add(Upgrade.websocket()); responseHeaders.add(this.key.accept()); if (protocol != null) { responseHeaders.add(SecWebSocketProtocol.from(protocol)); } if (!extensions.isEmpty()) { responseHeaders.add(SecWebSocketExtensions.from(extensions)); } responseHeaders.addAll(headers); return HttpResponse.from(HttpStatus.SWITCHING_PROTOCOLS, responseHeaders.bind()); } public HttpResponse<?> httpResponse(String protocol, HttpHeader... headers) { return httpResponse(protocol, FingerTrieSeq.<WebSocketExtension>empty(), FingerTrieSeq.of(headers)); } public HttpResponse<?> httpResponse(String protocol) { return httpResponse(protocol, FingerTrieSeq.<WebSocketExtension>empty(), FingerTrieSeq.<HttpHeader>empty()); } public HttpResponse<?> httpResponse(FingerTrieSeq<HttpHeader> headers) { return httpResponse(null, FingerTrieSeq.<WebSocketExtension>empty(), headers); } public HttpResponse<?> httpResponse(HttpHeader... headers) { return httpResponse(null, FingerTrieSeq.<WebSocketExtension>empty(), FingerTrieSeq.of(headers)); } public HttpResponse<?> httpResponse() { return httpResponse(null, FingerTrieSeq.<WebSocketExtension>empty(), FingerTrieSeq.<HttpHeader>empty()); } public WsResponse accept(WsEngineSettings settings, String protocol, FingerTrieSeq<HttpHeader> headers) { final FingerTrieSeq<WebSocketExtension> responseExtensions = settings.acceptExtensions(this.extensions); final HttpResponse<?> httpResponse = httpResponse(protocol, responseExtensions, headers); return new WsResponse(this.httpRequest, httpResponse, protocol, responseExtensions); } public WsResponse accept(WsEngineSettings settings, String protocol, HttpHeader... headers) { return accept(settings, protocol, FingerTrieSeq.of(headers)); } public WsResponse accept(WsEngineSettings settings, String protocol) { return accept(settings, protocol, FingerTrieSeq.<HttpHeader>empty()); } public WsResponse accept(WsEngineSettings settings, FingerTrieSeq<HttpHeader> headers) { return accept(settings, null, headers); } public WsResponse accept(WsEngineSettings settings, HttpHeader... headers) { return accept(settings, null, FingerTrieSeq.of(headers)); } public WsResponse accept(WsEngineSettings settings) { return accept(settings, null, FingerTrieSeq.<HttpHeader>empty()); } public WsResponse accept(HttpResponse<?> httpResponse, WsEngineSettings settings) { boolean connectionUpgrade = false; boolean upgradeWebSocket = false; SecWebSocketAccept accept = null; String protocol = null; FingerTrieSeq<WebSocketExtension> extensions = FingerTrieSeq.empty(); for (HttpHeader header : httpResponse.headers()) { if (header instanceof Connection && ((Connection) header).contains("Upgrade")) { connectionUpgrade = true; } else if (header instanceof Upgrade && ((Upgrade) header).supports(UpgradeProtocol.websocket())) { upgradeWebSocket = true; } else if (header instanceof SecWebSocketAccept) { accept = (SecWebSocketAccept) header; } else if (header instanceof SecWebSocketProtocol) { final FingerTrieSeq<String> protocols = ((SecWebSocketProtocol) header).protocols(); if (!protocols.isEmpty()) { protocol = protocols.head(); } } else if (header instanceof SecWebSocketExtensions) { extensions = ((SecWebSocketExtensions) header).extensions(); } } if (httpResponse.status().code() == 101 && connectionUpgrade && upgradeWebSocket && accept != null) { extensions = settings.acceptExtensions(extensions); return new WsResponse(this.httpRequest, httpResponse, protocol, extensions); } return null; } public static WsRequest from(Uri uri, FingerTrieSeq<String> protocols, FingerTrieSeq<WebSocketExtension> extensions, FingerTrieSeq<HttpHeader> headers) { final SecWebSocketKey key = SecWebSocketKey.generate(); final Builder<HttpHeader, FingerTrieSeq<HttpHeader>> requestHeaders = FingerTrieSeq.builder(); requestHeaders.add(Host.from(uri.authority())); requestHeaders.add(Connection.upgrade()); requestHeaders.add(Upgrade.websocket()); requestHeaders.add(SecWebSocketVersion.version13()); requestHeaders.add(key); if (!protocols.isEmpty()) { requestHeaders.add(SecWebSocketProtocol.from(protocols)); } if (!extensions.isEmpty()) { requestHeaders.add(SecWebSocketExtensions.from(extensions)); } if (headers != null) { requestHeaders.addAll(headers); } final UriPath requestPath = uri.path().isEmpty() ? UriPath.slash() : uri.path(); final Uri requestUri = Uri.from(null, null, requestPath, uri.query(), null); final HttpRequest<?> httpRequest = HttpRequest.get(requestUri, requestHeaders.bind()); return new WsRequest(httpRequest, key, protocols, extensions); } public static WsRequest from(Uri uri, FingerTrieSeq<String> protocols, HttpHeader... headers) { return from(uri, protocols, FingerTrieSeq.<WebSocketExtension>empty(), FingerTrieSeq.of(headers)); } public static WsRequest from(Uri uri, FingerTrieSeq<String> protocols) { return from(uri, protocols, FingerTrieSeq.<WebSocketExtension>empty(), FingerTrieSeq.<HttpHeader>empty()); } public static WsRequest from(Uri uri, HttpHeader... headers) { return from(uri, FingerTrieSeq.<String>empty(), FingerTrieSeq.<WebSocketExtension>empty(), FingerTrieSeq.of(headers)); } public static WsRequest from(Uri uri) { return from(uri, FingerTrieSeq.<String>empty(), FingerTrieSeq.<WebSocketExtension>empty(), FingerTrieSeq.<HttpHeader>empty()); } public static WsRequest from(HttpRequest<?> httpRequest) { boolean connectionUpgrade = false; boolean upgradeWebSocket = false; SecWebSocketKey key = null; FingerTrieSeq<String> protocols = FingerTrieSeq.empty(); FingerTrieSeq<WebSocketExtension> extensions = FingerTrieSeq.empty(); for (HttpHeader header : httpRequest.headers()) { if (header instanceof Connection && ((Connection) header).contains("Upgrade")) { connectionUpgrade = true; } else if (header instanceof Upgrade && ((Upgrade) header).supports(UpgradeProtocol.websocket())) { upgradeWebSocket = true; } else if (header instanceof SecWebSocketKey) { key = (SecWebSocketKey) header; } else if (header instanceof SecWebSocketProtocol) { protocols = ((SecWebSocketProtocol) header).protocols(); } else if (header instanceof SecWebSocketExtensions) { extensions = ((SecWebSocketExtensions) header).extensions(); } } if (connectionUpgrade && upgradeWebSocket && key != null) { return new WsRequest(httpRequest, key, protocols, extensions); } return null; } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsResponse.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import swim.collections.FingerTrieSeq; import swim.http.HttpRequest; import swim.http.HttpResponse; import swim.http.WebSocketExtension; /** * WebSocket handshake response. */ public class WsResponse { protected final HttpRequest<?> httpRequest; protected final HttpResponse<?> httpResponse; protected final String protocol; protected final FingerTrieSeq<WebSocketExtension> extensions; public WsResponse(HttpRequest<?> httpRequest, HttpResponse<?> httpResponse, String protocol, FingerTrieSeq<WebSocketExtension> extensions) { this.httpRequest = httpRequest; this.httpResponse = httpResponse; this.protocol = protocol; this.extensions = extensions; } public final HttpRequest<?> httpRequest() { return this.httpRequest; } public final HttpResponse<?> httpResponse() { return this.httpResponse; } public final String protocol() { return this.protocol; } public final FingerTrieSeq<WebSocketExtension> extensions() { return this.extensions; } public WsEngine clientEngine(WsEngineSettings settings) { return WsEngine.standardClientEngine().extensions(this.extensions, settings); } public WsEngine serverEngine(WsEngineSettings settings) { return WsEngine.standardServerEngine().extensions(this.extensions, settings); } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsStandardClientEngine.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import swim.http.WebSocketExtension; final class WsStandardClientEngine extends WsEngine { @Override public WsDecoder decoder() { return Ws.standardDecoder(); } @Override public WsEncoder encoder() { return Ws.standardEncoderMasked(); } @Override public WsEngine extension(WebSocketExtension extension, WsEngineSettings settings) { if ("permessage-deflate".equals(extension.name())) { return deflateClientEngine(extension, settings); } else { return this; } } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsStandardDecoder.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; public class WsStandardDecoder extends WsDecoder { }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsStandardEncoder.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; public abstract class WsStandardEncoder extends WsEncoder { }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsStandardEncoderMasked.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import java.util.concurrent.ThreadLocalRandom; final class WsStandardEncoderMasked extends WsStandardEncoder { @Override public boolean isMasked() { return true; } @Override public void maskingKey(byte[] maskingKey) { ThreadLocalRandom.current().nextBytes(maskingKey); } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsStandardEncoderUnmasked.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; final class WsStandardEncoderUnmasked extends WsStandardEncoder { @Override public boolean isMasked() { return false; } @Override public void maskingKey(byte[] maskingKey) { // nop } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsStandardServerEngine.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import swim.http.WebSocketExtension; final class WsStandardServerEngine extends WsEngine { @Override public WsDecoder decoder() { return Ws.standardDecoder(); } @Override public WsEncoder encoder() { return Ws.standardEncoderUnmasked(); } @Override public WsEngine extension(WebSocketExtension extension, WsEngineSettings settings) { if ("permessage-deflate".equals(extension.name())) { return deflateServerEngine(extension, settings); } else { return this; } } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsStatus.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import swim.codec.Debug; import swim.codec.Decoder; import swim.codec.Encoder; import swim.codec.Format; import swim.codec.InputBuffer; import swim.codec.Output; import swim.codec.OutputBuffer; import swim.util.Murmur3; public final class WsStatus implements Debug { final int code; final String reason; WsStatus(int code, String reason) { this.code = code; this.reason = reason; } public int code() { return this.code; } public String reason() { return this.reason; } public Encoder<?, WsStatus> encoder() { return new WsStatusEncoder(this); } public Encoder<?, WsStatus> encode(OutputBuffer<?> output) { return WsStatusEncoder.encode(output, this); } @Override public boolean equals(Object other) { if (this == other) { return true; } else if (other instanceof WsStatus) { final WsStatus that = (WsStatus) other; return this.code == that.code && this.reason.equals(that.reason); } return false; } @Override public int hashCode() { if (hashSeed == 0) { hashSeed = Murmur3.seed(WsStatus.class); } return Murmur3.mash(Murmur3.mix(Murmur3.mix(hashSeed, this.code), this.reason.hashCode())); } @Override public void debug(Output<?> output) { output = output.write("WsStatus").write('.').write("from").write('(').debug(code); if (!this.reason.isEmpty()) { output = output.write(", ").debug(this.reason); } output = output.write(')'); } @Override public String toString() { return Format.debug(this); } private static int hashSeed; public static WsStatus from(int code, String reason) { return new WsStatus(code, reason); } public static WsStatus from(int code) { return new WsStatus(code, ""); } public static Decoder<WsStatus> decoder() { return new WsStatusDecoder(); } public static Decoder<WsStatus> decode(InputBuffer input) { return WsStatusDecoder.decode(input); } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsStatusDecoder.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import swim.codec.Decoder; import swim.codec.DecoderException; import swim.codec.InputBuffer; import swim.codec.Utf8; final class WsStatusDecoder extends Decoder<WsStatus> { final int code; final Decoder<String> reason; final int step; WsStatusDecoder(int code, Decoder<String> reason, int step) { this.code = code; this.reason = reason; this.step = step; } WsStatusDecoder() { this(0, null, 1); } @Override public Decoder<WsStatus> feed(InputBuffer input) { return decode(input, this.code, this.reason, this.step); } static Decoder<WsStatus> decode(InputBuffer input, int code, Decoder<String> reason, int step) { if (step == 1 && input.isCont()) { code = input.head() << 8; input = input.step(); step = 2; } if (step == 2 && input.isCont()) { code |= input.head(); input = input.step(); step = 3; } if (step == 3) { if (reason == null) { reason = Utf8.parseString(input); } else { reason = reason.feed(input); } if (reason.isDone()) { return done(new WsStatus(code, reason.bind())); } else if (reason.isError()) { return reason.asError(); } } if (input.isDone()) { return error(new DecoderException("incomplete")); } else if (input.isError()) { return error(input.trap()); } return new WsStatusDecoder(code, reason, step); } static Decoder<WsStatus> decode(InputBuffer input) { return decode(input, 0, null, 1); } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsStatusEncoder.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import swim.codec.Encoder; import swim.codec.EncoderException; import swim.codec.OutputBuffer; import swim.codec.Utf8; final class WsStatusEncoder extends Encoder<Object, WsStatus> { final WsStatus status; final Encoder<?, ?> part; final int step; WsStatusEncoder(WsStatus status, Encoder<?, ?> part, int step) { this.status = status; this.part = part; this.step = step; } WsStatusEncoder(WsStatus status) { this(status, null, 1); } @Override public Encoder<Object, WsStatus> pull(OutputBuffer<?> output) { return encode(output, this.status, this.part, this.step); } static Encoder<Object, WsStatus> encode(OutputBuffer<?> output, WsStatus status, Encoder<?, ?> part, int step) { if (step == 1 && output.isCont()) { output = output.write(status.code >>> 8); step = 2; } if (step == 2 && output.isCont()) { output = output.write(status.code); step = 3; } if (step == 3) { if (part == null) { part = Utf8.writeString(status.reason, output); } else { part = part.pull(output); } if (part.isDone()) { return done(status); } else if (part.isError()) { return part.asError(); } } if (output.isDone()) { return error(new EncoderException("truncated")); } else if (output.isError()) { return error(output.trap()); } return new WsStatusEncoder(status, part, step); } static Encoder<Object, WsStatus> encode(OutputBuffer<?> output, WsStatus status) { return encode(output, status, null, 1); } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsText.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import swim.codec.Debug; import swim.codec.Encoder; import swim.codec.Format; import swim.codec.Output; import swim.codec.OutputBuffer; import swim.codec.Utf8; import swim.util.Murmur3; public final class WsText<T> extends WsData<T> implements Debug { final T value; final Encoder<?, ?> content; WsText(T value, Encoder<?, ?> content) { this.value = value; this.content = content; } @Override public boolean isDefined() { return this.value != null; } @Override public T get() { return this.value; } @Override public WsOpcode opcode() { return WsOpcode.TEXT; } @Override public Object payload() { return this.value; } @Override public Encoder<?, ?> contentEncoder(WsEncoder ws) { return this.content; } @Override public Encoder<?, ?> encodeContent(OutputBuffer<?> output, WsEncoder ws) { return this.content.pull(output); } @Override public boolean equals(Object other) { if (this == other) { return true; } else if (other instanceof WsText<?>) { final WsText<?> that = (WsText<?>) other; return (this.value == null ? that.value == null : this.value.equals(that.value)); } return false; } @Override public int hashCode() { if (hashSeed == 0) { hashSeed = Murmur3.seed(WsText.class); } return Murmur3.mash(Murmur3.mix(hashSeed, Murmur3.hash(this.value))); } @Override public void debug(Output<?> output) { output = output.write("WsText").write('.').write("from").write('('); if (this.value != null) { output = output.debug(this.value).write(", "); } output = output.debug(this.content).write(')'); } @Override public String toString() { return Format.debug(this); } private static int hashSeed; public static <T> WsText<T> from(T value, Encoder<?, ?> content) { return new WsText<T>(value, content); } public static <T> WsText<T> from(Encoder<?, ?> content) { return new WsText<T>(null, content); } public static WsText<String> from(String value) { return new WsText<String>(value, Utf8.stringWriter(value)); } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/WsValue.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.ws; import swim.codec.Debug; import swim.codec.Encoder; import swim.codec.Format; import swim.codec.Output; import swim.codec.OutputBuffer; import swim.util.Murmur3; public final class WsValue<T> extends WsData<T> implements Debug { final T value; WsValue(T value) { this.value = value; } @Override public boolean isDefined() { return true; } @Override public T get() { return this.value; } @Override public WsOpcode opcode() { return WsOpcode.INVALID; } @Override public Object payload() { return this.value; } @Override public Encoder<?, ?> contentEncoder(WsEncoder ws) { throw new UnsupportedOperationException(); } @Override public Encoder<?, ?> encodeContent(OutputBuffer<?> output, WsEncoder ws) { throw new UnsupportedOperationException(); } @Override public boolean equals(Object other) { if (this == other) { return true; } else if (other instanceof WsValue<?>) { final WsValue<?> that = (WsValue<?>) other; return (this.value == null ? that.value == null : this.value.equals(that.value)); } return false; } @Override public int hashCode() { if (hashSeed == 0) { hashSeed = Murmur3.seed(WsValue.class); } return Murmur3.mash(Murmur3.mix(hashSeed, Murmur3.hash(this.value))); } @Override public void debug(Output<?> output) { output = output.write("WsValue").write('.').write("from").write('(') .debug(this.value).write(')'); } @Override public String toString() { return Format.debug(this); } private static int hashSeed; public static <T> WsValue<T> from(T value) { return new WsValue<T>(value); } }
0
java-sources/ai/swim/swim-ws/3.10.0/swim
java-sources/ai/swim/swim-ws/3.10.0/swim/ws/package-info.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /** * WebSocket wire protocol model, decoders, and encoders. */ package swim.ws;
0
java-sources/ai/swim/swim-xml
java-sources/ai/swim/swim-xml/3.10.0/module-info.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /** * eXtensible Markup Language (XML) codec. */ module swim.xml { requires swim.util; requires transitive swim.codec; requires transitive swim.structure; exports swim.xml; }
0
java-sources/ai/swim/swim-xml/3.10.0/swim
java-sources/ai/swim/swim-xml/3.10.0/swim/xml/AttributeValueParser.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.xml; import swim.codec.Diagnostic; import swim.codec.Input; import swim.codec.Output; import swim.codec.Parser; final class AttributeValueParser<V> extends Parser<V> { final XmlParser<?, V> xml; final Output<V> output; final Parser<?> referenceParser; final int quote; final int step; AttributeValueParser(XmlParser<?, V> xml, Output<V> output, Parser<?> referenceParser, int quote, int step) { this.xml = xml; this.output = output; this.referenceParser = referenceParser; this.quote = quote; this.step = step; } @Override public Parser<V> feed(Input input) { return parse(input, this.xml, this.output, this.referenceParser, this.quote, this.step); } static <V> Parser<V> parse(Input input, XmlParser<?, V> xml, Output<V> output, Parser<?> referenceParser, int quote, int step) { int c = 0; if (step == 1) { if (input.isCont()) { c = input.head(); if ((c == '"' || c == '\'') && (quote == c || quote == 0)) { input = input.step(); if (output == null) { output = xml.textOutput(); } quote = c; step = 2; } else { return error(Diagnostic.expected("attribute value", input)); } } else if (input.isDone()) { return error(Diagnostic.expected("attribute value", input)); } } do { if (step == 2) { while (input.isCont()) { c = input.head(); if (c >= 0x20 && c != quote && c != '<' && c != '&') { input = input.step(); output = output.write(c); } else { break; } } if (input.isCont()) { if (c == quote) { input = input.step(); return done(output.bind()); } else if (c == '&') { step = 3; } else { return error(Diagnostic.unexpected(input)); } } else if (input.isDone()) { return error(Diagnostic.unexpected(input)); } } if (step == 3) { if (referenceParser == null) { referenceParser = xml.parseReference(input, output); } else { referenceParser = referenceParser.feed(input); } if (referenceParser.isDone()) { referenceParser = null; step = 2; continue; } else if (referenceParser.isError()) { return referenceParser.asError(); } } break; } while (true); if (input.isError()) { return error(input.trap()); } return new AttributeValueParser<V>(xml, output, referenceParser, quote, step); } static <V> Parser<V> parse(Input input, XmlParser<?, V> xml, Output<V> output) { return parse(input, xml, output, null, 0, 1); } static <V> Parser<V> parse(Input input, XmlParser<?, V> xml) { return parse(input, xml, null, null, 0, 1); } }