Create startup content based on request HTTP accept header

Startup request returns HTML for web client and JSON for others.
RWT.getClient() was used in LifeCycleServiceHandler.sendStartupContent
to switch the content. As RWT.getClient() requires UI session, we have
to create dummy UI session in startup request. Using HTTP accept header
instead will allow as to remove this dummy UI session.

Change-Id: Ie5c322e9ca410396932aa10343b1c8ef9bcdc604
diff --git a/bundles/org.eclipse.rap.rwt/src/org/eclipse/rap/rwt/internal/service/LifeCycleServiceHandler.java b/bundles/org.eclipse.rap.rwt/src/org/eclipse/rap/rwt/internal/service/LifeCycleServiceHandler.java
index ed4431c..a6a229f 100644
--- a/bundles/org.eclipse.rap.rwt/src/org/eclipse/rap/rwt/internal/service/LifeCycleServiceHandler.java
+++ b/bundles/org.eclipse.rap.rwt/src/org/eclipse/rap/rwt/internal/service/LifeCycleServiceHandler.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2002, 2014 Innoopract Informationssysteme GmbH and others.
+ * Copyright (c) 2002, 2015 Innoopract Informationssysteme GmbH and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -33,8 +33,6 @@
 
 import org.eclipse.rap.json.JsonObject;
 import org.eclipse.rap.json.JsonValue;
-import org.eclipse.rap.rwt.RWT;
-import org.eclipse.rap.rwt.client.WebClient;
 import org.eclipse.rap.rwt.internal.lifecycle.RequestCounter;
 import org.eclipse.rap.rwt.internal.protocol.ClientMessage;
 import org.eclipse.rap.rwt.internal.protocol.ProtocolMessageWriter;
@@ -42,6 +40,7 @@
 import org.eclipse.rap.rwt.internal.protocol.RequestMessage;
 import org.eclipse.rap.rwt.internal.protocol.ResponseMessage;
 import org.eclipse.rap.rwt.internal.remote.MessageChainReference;
+import org.eclipse.rap.rwt.internal.util.HTTP;
 import org.eclipse.rap.rwt.service.ServiceHandler;
 import org.eclipse.rap.rwt.service.UISession;
 
@@ -112,13 +111,14 @@
     }
   }
 
-  private void sendStartupContent( ServletRequest request, HttpServletResponse response )
+  private void sendStartupContent( HttpServletRequest request, HttpServletResponse response )
     throws IOException
   {
-    if( RWT.getClient() instanceof WebClient ) {
-      startupPage.send( response );
-    } else {
+    String accept = request.getHeader( HTTP.HEADER_ACCEPT );
+    if( accept != null && accept.contains( HTTP.CONTENT_TYPE_JSON ) ) {
       StartupJson.send( response );
+    } else {
+      startupPage.send( response );
     }
   }
 
diff --git a/bundles/org.eclipse.rap.rwt/src/org/eclipse/rap/rwt/internal/util/HTTP.java b/bundles/org.eclipse.rap.rwt/src/org/eclipse/rap/rwt/internal/util/HTTP.java
index 82d9724..438db3b 100644
--- a/bundles/org.eclipse.rap.rwt/src/org/eclipse/rap/rwt/internal/util/HTTP.java
+++ b/bundles/org.eclipse.rap.rwt/src/org/eclipse/rap/rwt/internal/util/HTTP.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2002, 2012 Innoopract Informationssysteme GmbH and others.
+ * Copyright (c) 2002, 2015 Innoopract Informationssysteme GmbH and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -24,8 +24,10 @@
   public final static String CHARSET_UTF_8 = "UTF-8";
   public static final String METHOD_GET = "GET";
   public static final String METHOD_POST = "POST";
+  public static final String HEADER_ACCEPT = "Accept";
 
   private HTTP() {
     // prevent instantiation
   }
+
 }
diff --git a/tests/org.eclipse.rap.rwt.test/src/org/eclipse/rap/rwt/internal/service/LifeCycleServiceHandler_Test.java b/tests/org.eclipse.rap.rwt.test/src/org/eclipse/rap/rwt/internal/service/LifeCycleServiceHandler_Test.java
index cd3f6a8..1d0ac94 100644
--- a/tests/org.eclipse.rap.rwt.test/src/org/eclipse/rap/rwt/internal/service/LifeCycleServiceHandler_Test.java
+++ b/tests/org.eclipse.rap.rwt.test/src/org/eclipse/rap/rwt/internal/service/LifeCycleServiceHandler_Test.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2002, 2014 Innoopract Informationssysteme GmbH and others.
+ * Copyright (c) 2002, 2015 Innoopract Informationssysteme GmbH and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -41,7 +41,6 @@
 
 import org.eclipse.rap.json.JsonArray;
 import org.eclipse.rap.json.JsonObject;
-import org.eclipse.rap.rwt.client.Client;
 import org.eclipse.rap.rwt.client.WebClient;
 import org.eclipse.rap.rwt.internal.application.ApplicationContextImpl;
 import org.eclipse.rap.rwt.internal.lifecycle.RequestCounter;
@@ -54,6 +53,7 @@
 import org.eclipse.rap.rwt.internal.remote.MessageChainReference;
 import org.eclipse.rap.rwt.internal.remote.MessageFilter;
 import org.eclipse.rap.rwt.internal.remote.MessageFilterChain;
+import org.eclipse.rap.rwt.internal.util.HTTP;
 import org.eclipse.rap.rwt.service.ServiceHandler;
 import org.eclipse.rap.rwt.service.UISession;
 import org.eclipse.rap.rwt.service.UISessionEvent;
@@ -299,9 +299,19 @@
   }
 
   @Test
-  public void testStartupJson_forOtherClients() throws IOException {
+  public void testStartupContent_withHtmlAcceptHeader() throws IOException {
     Fixture.fakeNewGetRequest();
-    Fixture.fakeClient( mock( Client.class ) );
+    getRequest().setHeader( HTTP.HEADER_ACCEPT, HTTP.CONTENT_TYPE_HTML );
+
+    service( serviceHandler );
+
+    verify( startupPage ).send( getResponse() );
+  }
+
+  @Test
+  public void testStartupContent_withJsonAcceptHeader() throws IOException {
+    Fixture.fakeNewGetRequest();
+    getRequest().setHeader( HTTP.HEADER_ACCEPT, HTTP.CONTENT_TYPE_JSON );
 
     service( serviceHandler );
 
@@ -310,9 +320,8 @@
   }
 
   @Test
-  public void testStartupPage_forWebClient() throws IOException {
+  public void testStartupContent_withoutAcceptHeader() throws IOException {
     Fixture.fakeNewGetRequest();
-    Fixture.fakeClient( mock( WebClient.class ) );
 
     service( serviceHandler );