001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.protocol.ws.api;
018
019import java.util.List;
020
021import org.apache.wicket.Application;
022import org.apache.wicket.ajax.WicketAjaxJQueryResourceReference;
023import org.apache.wicket.markup.head.HeaderItem;
024import org.apache.wicket.markup.head.JavaScriptHeaderItem;
025import org.apache.wicket.request.resource.JavaScriptResourceReference;
026import org.apache.wicket.request.resource.ResourceReference;
027
028/**
029 * A resource reference that provides the JavaScript that may be used to create WebSocket
030 * connections in the browser. The benefit over usage of plain <code>window.WebSocket<code>
031 * is that it supports handling of Wicket's <ajax-response> responses.
032 *
033 * @since 6.0
034 */
035public class WicketWebSocketJQueryResourceReference extends JavaScriptResourceReference
036{
037        private static final long serialVersionUID = 1;
038
039        private static final WicketWebSocketJQueryResourceReference instance = new WicketWebSocketJQueryResourceReference();
040
041        /**
042         * @return the singleton instance
043         */
044        public static WicketWebSocketJQueryResourceReference get()
045        {
046                return instance;
047        }
048
049        private WicketWebSocketJQueryResourceReference()
050        {
051                super(WicketWebSocketJQueryResourceReference.class, "res/js/wicket-websocket-jquery.js");
052        }
053
054        @Override
055        public List<HeaderItem> getDependencies()
056        {
057                final ResourceReference wicketAjaxReference;
058                if (Application.exists())
059                {
060                        wicketAjaxReference = Application.get().getJavaScriptLibrarySettings().getWicketAjaxReference();
061                }
062                else
063                {
064                        wicketAjaxReference = WicketAjaxJQueryResourceReference.get();
065                }
066                List<HeaderItem> dependencies = super.getDependencies();
067                dependencies.add(JavaScriptHeaderItem.forReference(wicketAjaxReference));
068                return dependencies;
069        }
070}