32 | * {@link URLStreamHandlerFactory} is a singleton that is registered using the 33 | * static method 34 | * {@link URL#setURLStreamHandlerFactory(URLStreamHandlerFactory)}. Also, 35 | * the {@link URL} objects can only be created for the following protocols: 36 | * -- http, https, file, ftp, and jar. In order to install protocol handlers 37 | * for other protocols, one has to hijack or override the system's singleton 38 | * {@link URLStreamHandlerFactory} instance with a custom implementation. The 39 | * objective of this class is to make the {@link URLStreamHandler} registration 40 | * for other protocols such as ws, wss, etc. feasible without hijacking the 41 | * system's {@link URLStreamHandlerFactory}. 42 | *
43 | */
44 | public abstract class URLStreamHandlerFactorySpi implements URLStreamHandlerFactory {
45 |
46 | /**
47 | * Returns a list of supported protocols. This can be used to instantiate
48 | * appropriate {@link URLStreamHandler} objects based on the protocol.
49 | *
50 | * @return list of supported protocols
51 | */
52 | public abstract Collection
35 | * Once the connection is closed, a new {@link WebSocketMessageReader} should
36 | * be obtained using the aforementioned methods after the connection has been
37 | * established. Using the old reader will result in IOException.
38 | */
39 | public abstract class WebSocketMessageWriter {
40 |
41 | /**
42 | * Sends a text message using the specified payload. Trying to write
43 | * after the underlying connection has been closed will result in an
44 | * IOException.
45 | *
46 | * @param src CharSequence payload of the message
47 | * @throws IOException if the connection is not open or if the connection
48 | * has been closed
49 | */
50 | public abstract void writeText(CharSequence src) throws IOException;
51 |
52 | /**
53 | * Sends a binary message using the specified payload.
54 | *
55 | * @param src ByteBuffer payload of the message
56 | * @throws IOException if the connection is not open or if the connection
57 | * has been closed
58 | */
59 | public abstract void writeBinary(ByteBuffer src) throws IOException;
60 | }
61 |
--------------------------------------------------------------------------------
/cloudsdk/src/main/java/org/kaazing/net/ws/impl/WsExtensionParameterValuesSpiImpl.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2007-2014 Kaazing Corporation. All rights reserved.
3 | *
4 | * Licensed to the Apache Software Foundation (ASF) under one
5 | * or more contributor license agreements. See the NOTICE file
6 | * distributed with this work for additional information
7 | * regarding copyright ownership. The ASF licenses this file
8 | * to you under the Apache License, Version 2.0 (the
9 | * "License"); you may not use this file except in compliance
10 | * with the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing,
15 | * software distributed under the License is distributed on an
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | * KIND, either express or implied. See the License for the
18 | * specific language governing permissions and limitations
19 | * under the License.
20 | */
21 |
22 | package org.kaazing.net.ws.impl;
23 |
24 | import org.kaazing.net.ws.WebSocketExtension.Parameter;
25 | import org.kaazing.net.ws.impl.spi.WebSocketExtensionParameterValuesSpi;
26 |
27 | import java.util.Collection;
28 | import java.util.Collections;
29 | import java.util.HashMap;
30 | import java.util.Map;
31 | import java.util.Set;
32 |
33 | import static java.util.Collections.unmodifiableSet;
34 |
35 | public final class WsExtensionParameterValuesSpiImpl extends WebSocketExtensionParameterValuesSpi {
36 |
37 | private final Map
34 | * WsExtensionParameterValues is used to cache extension parameters as
35 | * name-value pairs in a very generic type-safe way. The implementations of
36 | * {@link WebSocket#connect()} and {@link WsURLConnection#connect()} invoke
37 | * {@link WebSocketExtensionFactorySpi#createWsExtension(WebSocketExtensionParameterValuesSpi)}
38 | * method and pass in all the extension parameters that have been earlier set
39 | * by the developer for the enabled extensions.
40 | */
41 | public abstract class WebSocketExtensionParameterValuesSpi {
42 | /**
43 | * Returns the collection of {@link Parameter} objects of a
44 | * {@link WebSocketExtension} that have been set. Returns an empty
45 | * Collection if no parameters belonging to the extension have been set.
46 | *
47 | * @return Collection
29 | * A WebSocket Extension implementation consists of the following:
30 | *
37 | * Every supported extension will require implementing the aforementioned
38 | * classes. A subset of the supported extensions will be enabled by the
39 | * application developer.
40 | *
41 | * The enabled extensions are included on the wire during the handshake for
42 | * the client and the server to negotiate.
43 | *
44 | * The successfully negotiated extensions are then added to the WebSocket
45 | * message processing pipeline.
46 | *
47 | * @see RevalidateExtension
48 | */
49 | public abstract class WebSocketExtensionSpi {
50 |
51 | public abstract WebSocketExtensionHandlerSpi createHandler();
52 | }
53 |
--------------------------------------------------------------------------------
/cloudsdk/src/main/java/org/kaazing/net/ws/impl/url/WssURLStreamHandlerFactorySpiImpl.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2007-2014 Kaazing Corporation. All rights reserved.
3 | *
4 | * Licensed to the Apache Software Foundation (ASF) under one
5 | * or more contributor license agreements. See the NOTICE file
6 | * distributed with this work for additional information
7 | * regarding copyright ownership. The ASF licenses this file
8 | * to you under the Apache License, Version 2.0 (the
9 | * "License"); you may not use this file except in compliance
10 | * with the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing,
15 | * software distributed under the License is distributed on an
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | * KIND, either express or implied. See the License for the
18 | * specific language governing permissions and limitations
19 | * under the License.
20 | */
21 |
22 | package org.kaazing.net.ws.impl.url;
23 |
24 | import java.net.URLStreamHandler;
25 | import java.util.Collection;
26 |
27 | import static java.util.Arrays.asList;
28 | import static java.util.Collections.unmodifiableList;
29 |
30 | public class WssURLStreamHandlerFactorySpiImpl extends WsURLStreamHandlerFactorySpiImpl {
31 | private static final Collection
36 | * {@code
37 | *
38 | * BasicChallengeHandler basicChallengeHandler = ChallengeHandlerLoader.load(BasicChallengeHandler.class);
39 | * LoginHandler loginHandler = new LoginHandler() {
40 | * public PasswordAuthentication getCredentials() {
41 | * // Obtain credentials in an application-specific manner
42 | * }
43 | * }
44 | * basicChallengeHandler.setLoginHandler(loginHandler);
45 | *
46 | * }
47 | *
48 | */
49 | public abstract class LoginHandler {
50 |
51 | /**
52 | * Default constructor.
53 | */
54 | protected LoginHandler() {
55 | }
56 |
57 | /**
58 | * Gets the password authentication credentials from an arbitrary source.
59 | * @return the password authentication obtained.
60 | */
61 | public abstract PasswordAuthentication getCredentials();
62 |
63 | }
--------------------------------------------------------------------------------
/cloudsdk/src/main/java/org/kaazing/net/impl/auth/BasicChallengeResponseFactory.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2007-2014 Kaazing Corporation. All rights reserved.
3 | *
4 | * Licensed to the Apache Software Foundation (ASF) under one
5 | * or more contributor license agreements. See the NOTICE file
6 | * distributed with this work for additional information
7 | * regarding copyright ownership. The ASF licenses this file
8 | * to you under the Apache License, Version 2.0 (the
9 | * "License"); you may not use this file except in compliance
10 | * with the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing,
15 | * software distributed under the License is distributed on an
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | * KIND, either express or implied. See the License for the
18 | * specific language governing permissions and limitations
19 | * under the License.
20 | */
21 |
22 | package org.kaazing.net.impl.auth;
23 |
24 | import org.kaazing.gateway.client.util.Base64Util;
25 | import org.kaazing.gateway.client.util.WrappedByteBuffer;
26 | import org.kaazing.net.auth.ChallengeHandler;
27 | import org.kaazing.net.auth.ChallengeResponse;
28 |
29 | import java.net.PasswordAuthentication;
30 | import java.util.Arrays;
31 |
32 | public class BasicChallengeResponseFactory {
33 |
34 | public static ChallengeResponse create(PasswordAuthentication creds, ChallengeHandler nextChallengeHandler) {
35 | String unencoded = String.format("%s:%s", creds.getUserName(), new String(creds.getPassword()));
36 | String response = String.format("Basic %s", Base64Util.encode(WrappedByteBuffer.wrap(unencoded.getBytes())));
37 | Arrays.fill(creds.getPassword(), (char) 0);
38 | return new ChallengeResponse(response.toCharArray(), nextChallengeHandler);
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/cloudsdk/src/main/java/org/kaazing/net/impl/auth/RealmUtils.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2007-2014 Kaazing Corporation. All rights reserved.
3 | *
4 | * Licensed to the Apache Software Foundation (ASF) under one
5 | * or more contributor license agreements. See the NOTICE file
6 | * distributed with this work for additional information
7 | * regarding copyright ownership. The ASF licenses this file
8 | * to you under the Apache License, Version 2.0 (the
9 | * "License"); you may not use this file except in compliance
10 | * with the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing,
15 | * software distributed under the License is distributed on an
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | * KIND, either express or implied. See the License for the
18 | * specific language governing permissions and limitations
19 | * under the License.
20 | */
21 |
22 | package org.kaazing.net.impl.auth;
23 |
24 | import org.kaazing.net.auth.ChallengeRequest;
25 |
26 | import java.util.regex.Matcher;
27 | import java.util.regex.Pattern;
28 |
29 | public final class RealmUtils {
30 |
31 | private RealmUtils() {
32 | // prevent object creation
33 | }
34 |
35 | private static final String REALM_REGEX = "(.*)\\s?(?i:realm=)(\"(.*)\")(.*)";
36 | private static final Pattern REALM_PATTERN= Pattern.compile(REALM_REGEX);
37 |
38 | /**
39 | * A realm parameter is a valid authentication parameter for all authentication schemes
40 | * according to RFC 2617 Section 2.1".
41 | *
42 | *
43 | * The realm directive (case-insensitive) is required for all
44 | * authentication schemes that issue a challenge. The realm value
45 | * (case-sensitive), in combination with the canonical root URL (the
46 | * absoluteURI for the server whose abs_path is empty) of the server
47 | * being accessed, defines the protection space.
48 | *
49 | *
50 | * @param challengeRequest the challenge request to extract a realm from
51 | *
52 | * @return the unquoted realm parameter value if present, or {@code null} if no such parameter exists.
53 | */
54 | public static String getRealm(ChallengeRequest challengeRequest) {
55 | String authenticationParameters = challengeRequest.getAuthenticationParameters();
56 | if ( authenticationParameters == null) {
57 | return null;
58 | }
59 | Matcher m = REALM_PATTERN.matcher(authenticationParameters);
60 | if ( m.matches() && m.groupCount()>=3) {
61 | return m.group(3);
62 | }
63 | return null;
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/cloudsdk/src/main/java/org/kaazing/net/sse/SseEventType.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2007-2014 Kaazing Corporation. All rights reserved.
3 | *
4 | * Licensed to the Apache Software Foundation (ASF) under one
5 | * or more contributor license agreements. See the NOTICE file
6 | * distributed with this work for additional information
7 | * regarding copyright ownership. The ASF licenses this file
8 | * to you under the Apache License, Version 2.0 (the
9 | * "License"); you may not use this file except in compliance
10 | * with the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing,
15 | * software distributed under the License is distributed on an
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | * KIND, either express or implied. See the License for the
18 | * specific language governing permissions and limitations
19 | * under the License.
20 | */
21 |
22 | package org.kaazing.net.sse;
23 |
24 | public enum SseEventType {
25 | EOS, EMPTY, DATA;
26 | }
--------------------------------------------------------------------------------
/cloudsdk/src/main/java/org/kaazing/net/sse/SseException.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2007-2014 Kaazing Corporation. All rights reserved.
3 | *
4 | * Licensed to the Apache Software Foundation (ASF) under one
5 | * or more contributor license agreements. See the NOTICE file
6 | * distributed with this work for additional information
7 | * regarding copyright ownership. The ASF licenses this file
8 | * to you under the Apache License, Version 2.0 (the
9 | * "License"); you may not use this file except in compliance
10 | * with the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing,
15 | * software distributed under the License is distributed on an
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | * KIND, either express or implied. See the License for the
18 | * specific language governing permissions and limitations
19 | * under the License.
20 | */
21 |
22 | package org.kaazing.net.sse;
23 |
24 | import java.io.IOException;
25 |
26 | public class SseException extends IOException {
27 |
28 | private static final long serialVersionUID = 1L;
29 |
30 | public SseException(String reason) {
31 | super(reason);
32 | }
33 |
34 | public SseException(Exception ex) {
35 | super(ex);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/cloudsdk/src/main/java/org/kaazing/net/sse/impl/AuthenticatedEventSourceFactory.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Portions of this file copyright (c) 2007-2014 Kaazing Corporation.
3 | * All rights reserved.
4 | *
5 | * Licensed to the Apache Software Foundation (ASF) under one
6 | * or more contributor license agreements. See the NOTICE file
7 | * distributed with this work for additional information
8 | * regarding copyright ownership. The ASF licenses this file
9 | * to you under the Apache License, Version 2.0 (the
10 | * "License"); you may not use this file except in compliance
11 | * with the License. You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing,
16 | * software distributed under the License is distributed on an
17 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 | * KIND, either express or implied. See the License for the
19 | * specific language governing permissions and limitations
20 | * under the License.
21 | */
22 |
23 | package org.kaazing.net.sse.impl;
24 |
25 | import org.kaazing.net.sse.SseEventSource;
26 |
27 | import java.net.URI;
28 | import java.net.URISyntaxException;
29 |
30 | import io.particle.android.sdk.cloud.ParticleCloud;
31 |
32 |
33 | public class AuthenticatedEventSourceFactory extends DefaultEventSourceFactory {
34 |
35 | private final ParticleCloud cloud;
36 |
37 | public AuthenticatedEventSourceFactory(ParticleCloud cloud) {
38 | this.cloud = cloud;
39 | }
40 |
41 | @Override
42 | public SseEventSource createEventSource(URI location) throws URISyntaxException {
43 |
44 | String scheme = location.getScheme();
45 | if (!scheme.toLowerCase().equals("sse") &&
46 | !scheme.toLowerCase().equals("http") &&
47 | !scheme.toLowerCase().equals("https")) {
48 | String s = String.format("Incorrect scheme or protocol '%s'", scheme);
49 | throw new URISyntaxException(location.toString(), s);
50 | }
51 |
52 | SseEventSourceImpl eventSource = new AuthenticatedSseEventSourceImpl(location, cloud);
53 |
54 | // Set up the defaults from the factory.
55 | eventSource.setFollowRedirect(getDefaultFollowRedirect());
56 | eventSource.setRetryTimeout(getDefaultRetryTimeout());
57 |
58 | return eventSource;
59 | }
60 |
61 | }
62 |
--------------------------------------------------------------------------------
/cloudsdk/src/main/java/org/kaazing/net/sse/impl/SseEventStreamListener.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2007-2014 Kaazing Corporation. All rights reserved.
3 | *
4 | * Licensed to the Apache Software Foundation (ASF) under one
5 | * or more contributor license agreements. See the NOTICE file
6 | * distributed with this work for additional information
7 | * regarding copyright ownership. The ASF licenses this file
8 | * to you under the Apache License, Version 2.0 (the
9 | * "License"); you may not use this file except in compliance
10 | * with the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing,
15 | * software distributed under the License is distributed on an
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | * KIND, either express or implied. See the License for the
18 | * specific language governing permissions and limitations
19 | * under the License.
20 | */
21 |
22 | package org.kaazing.net.sse.impl;
23 |
24 | public interface SseEventStreamListener {
25 |
26 | void streamOpened();
27 | void messageReceived(String eventName, String data);
28 | void streamErrored(Exception exception);
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/cloudsdk/src/main/java/org/kaazing/net/sse/impl/SsePayload.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2007-2014 Kaazing Corporation. All rights reserved.
3 | *
4 | * Licensed to the Apache Software Foundation (ASF) under one
5 | * or more contributor license agreements. See the NOTICE file
6 | * distributed with this work for additional information
7 | * regarding copyright ownership. The ASF licenses this file
8 | * to you under the Apache License, Version 2.0 (the
9 | * "License"); you may not use this file except in compliance
10 | * with the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing,
15 | * software distributed under the License is distributed on an
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | * KIND, either express or implied. See the License for the
18 | * specific language governing permissions and limitations
19 | * under the License.
20 | */
21 |
22 | package org.kaazing.net.sse.impl;
23 |
24 | public class SsePayload {
25 |
26 | private String _eventName;
27 | private String _data;
28 |
29 | public SsePayload(String eventName, String data) {
30 | _eventName = eventName;
31 | _data = data;
32 | }
33 |
34 | public String getData() {
35 | return _data;
36 | }
37 |
38 | public String getEventName() {
39 | return _eventName;
40 | }
41 |
42 | public void setData(String data) {
43 | _data = data;
44 | }
45 |
46 | public void setEventName(String eventName) {
47 | if ((eventName == null) || (eventName.trim().length() == 0)) {
48 | eventName = "message";
49 | }
50 |
51 | _eventName = eventName;
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/cloudsdk/src/main/java/org/kaazing/net/sse/impl/legacy/EventSourceAdapter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2007-2014 Kaazing Corporation. All rights reserved.
3 | *
4 | * Licensed to the Apache Software Foundation (ASF) under one
5 | * or more contributor license agreements. See the NOTICE file
6 | * distributed with this work for additional information
7 | * regarding copyright ownership. The ASF licenses this file
8 | * to you under the Apache License, Version 2.0 (the
9 | * "License"); you may not use this file except in compliance
10 | * with the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing,
15 | * software distributed under the License is distributed on an
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | * KIND, either express or implied. See the License for the
18 | * specific language governing permissions and limitations
19 | * under the License.
20 | */
21 |
22 | package org.kaazing.net.sse.impl.legacy;
23 |
24 |
25 | /**
26 | * An adapter class for {@link EventSourceListener}. Use this as a base class to
27 | * override selected methods only.
28 | *
29 | */
30 | public class EventSourceAdapter implements EventSourceListener {
31 |
32 | @Override
33 | public void onError(EventSourceEvent error) {
34 |
35 | }
36 |
37 | @Override
38 | public void onMessage(EventSourceEvent message) {
39 |
40 | }
41 |
42 | @Override
43 | public void onOpen(EventSourceEvent open) {
44 |
45 | }
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/cloudsdk/src/main/java/org/kaazing/net/sse/impl/legacy/EventSourceEvent.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2007-2014 Kaazing Corporation. All rights reserved.
3 | *
4 | * Licensed to the Apache Software Foundation (ASF) under one
5 | * or more contributor license agreements. See the NOTICE file
6 | * distributed with this work for additional information
7 | * regarding copyright ownership. The ASF licenses this file
8 | * to you under the Apache License, Version 2.0 (the
9 | * "License"); you may not use this file except in compliance
10 | * with the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing,
15 | * software distributed under the License is distributed on an
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | * KIND, either express or implied. See the License for the
18 | * specific language governing permissions and limitations
19 | * under the License.
20 | */
21 |
22 | package org.kaazing.net.sse.impl.legacy;
23 |
24 | import java.util.EventObject;
25 | import java.util.logging.Logger;
26 |
27 | /**
28 | * This class represents events generated by the EventSource object
29 | *
30 | */
31 | public class EventSourceEvent extends EventObject {
32 | private static final String CLASS_NAME = EventSourceEvent.class.getName();
33 | private static final Logger LOG = Logger.getLogger(CLASS_NAME);
34 |
35 | private static final long serialVersionUID = -3654347840399311101L;
36 | private final Type type;
37 | private final String data;
38 |
39 | /**
40 | * Type of the EventSourceEvent.
41 | */
42 | public enum Type {
43 | OPEN, MESSAGE, ERROR
44 | }
45 |
46 | /**
47 | *
48 | * @param source
49 | * @param type
50 | */
51 | public EventSourceEvent(Object source, Type type) {
52 | this(source, type, null);
53 | }
54 |
55 | public EventSourceEvent(Object source, Type type, String message) {
56 | super(source);
57 | LOG.entering(CLASS_NAME, "
31 | *
36 | *