Menu
Amazon AppStream
Developer Guide

This documentation is for an older version of Amazon AppStream. For information about the latest version, see the Amazon AppStream 2.0 Developer Guide.

Getting Authorization to Connect to Your Application

The first activity in your client is to get the Entitlement URL from the endpoint of the entitlement service. The entitlement service needs information in order to provide the Entitlement URL.

The following excerpt from the example client in the Amazon AppStream SDK illustrates this step. The excerpt is from the makeQuery method of DesQuery.java.

Copy
public void makeQuery(String address, final String appid, final String userid) { if (!address.startsWith("http")) { address = "http://" + address; } if (!address.endsWith("/")) { address += "/"; } // if there's no path, add api/entitlements/ if (!address.matches("http[s]?://.+/.+")) { address += "api/entitlements/"; } final String cleanAddress = address; Thread httpThread = new Thread() { @Override public void run() { HttpParams httpParameters = new BasicHttpParams(); // timeout for a connection in ms HttpConnectionParams.setConnectionTimeout(httpParameters, 1200); // timeout when waiting for data HttpConnectionParams.setSoTimeout(httpParameters, 1500); HttpClient client = new DefaultHttpClient(); HttpPost request = new HttpPost(); String queryURL = cleanAddress+appid; try { request.setURI(new URI(queryURL)); } catch (URISyntaxException e) { sendError("Either address or appid isn't in the correct format. Generated URL:"+queryURL+" Error:"+e.getLocalizedMessage()); return; } request.setHeader("Authorization", "Username"+userid ); request.setHeader("Content-Type", "application/x-www-form-urlencoded" ); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); nameValuePairs.add(new BasicNameValuePair("terminatePrevious", "true")); String entitlementResult = null; try { request.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = client.execute(request); if (response.getStatusLine().getStatusCode()<200 || response.getStatusLine().getStatusCode()>201) { if (response.getStatusLine().getStatusCode()==503) { sendError("All of our streaming servers are currently in use. Please try again in a few minutes. [503]"); } else if (response.getStatusLine().getStatusCode()==404) { sendError("The link to the streaming server has expired. [404]"); } else { sendError("Query failed with response: "+response.getStatusLine()); } return; } BufferedReader result = new BufferedReader( new InputStreamReader( response.getEntity().getContent() ) ); final String url = result.readLine().trim(); Log.v(TAG,"Resulting URL: "+url); result.close(); HttpGet entitlementRequest = new HttpGet(url); response = client.execute(entitlementRequest); result = new BufferedReader(new InputStreamReader( response.getEntity().getContent() ) ); StringBuilder buffer = new StringBuilder(1024); char[] charBuf = new char[400]; while (result.read(charBuf,0,400)>0) { buffer.append(charBuf); } entitlementResult = buffer.toString(); JSONObject object = (JSONObject) new JSONTokener(entitlementResult).nextValue(); final String sessionID = object.optString("sessionID"); final String ec2Host = object.optString("ec2Host"); if (sessionID==null || ec2Host==null) { final String message = object.optString("message"); if (message!=null) { sendError("Entitlement query failed with response: "+message+"["+response.getStatusLine()+"]"); return; } sendError("Error parsing entitlement result: "+entitlementResult); return; } mActivity.runOnUiThread(new Runnable(){ @Override public void run() { String url = String.format(Locale.US,"%s:80?sessionId=%s",ec2Host,sessionID); Log.v(TAG,"Sending host url "+url); mListener.onDesQuerySuccess(url); }}); } catch (ClientProtocolException e) { sendError("Protocol Exception: "+e.getLocalizedMessage()); } catch (IOException e) { sendError("Problem With Connection: "+e.getLocalizedMessage()); } catch (JSONException e) { sendError("Error reading entitlement result: "+e.getLocalizedMessage()+" Result: "+entitlementResult); } } }; httpThread.start(); }

The function is successful when the entitlement service calls a callback function with the Entitlement URL to start the session with the application.

The following excerpt from the example client in the Amazon AppStream SDK illustrates the callback function is called to connect the client to the application. The excerpt is from the onDesQuerySuccess method of SampleClietActivity.java.

Copy
public void onDesQuerySuccess(String address) { AppStreamInterface.connect(address); AppStreamInterface.newFrame(); }