@Test public void executeScript() throws Exception { NCMBScriptService scriptService = (NCMBScriptService) NCMB.factory(NCMB.ServiceType.SCRIPT); byte[] result = scriptService.executeScript( "testScript.js", NCMBScript.MethodType.GET, null, null, null, mScriptUrl); Assert.assertEquals("hello", new String(result, "UTF-8")); }
public byte[] executeScript(String scriptName, MethodType method, Map<String, String> header, JSONObject body, JSONObject query, String baseUrl) throws NCMBException { String scriptUrl; if (baseUrl != null && baseUrl.length() > 0) { scriptUrl = baseUrl + "/" + scriptName; } else { scriptUrl = DEFAULT_SCRIPT_DOMAIN_URL + "/" + DEFAULT_SCRIPT_API_VERSION + "/" + mServicePath + "/" + scriptName; } byte[] responseByte = null; HttpURLConnection urlConnection = null; String type; try { switch (method) { case POST: type = NCMBRequest.HTTP_METHOD_POST; break; case PUT: type = NCMBRequest.HTTP_METHOD_PUT; break; case GET: type = NCMBRequest.HTTP_METHOD_GET; break; case DELETE: type = NCMBRequest.HTTP_METHOD_DELETE; break; default: throw new IllegalArgumentException("Invalid methodType"); } String content = null; if (body != null) { content = body.toString(); } if (mContext.sessionToken == null) { mContext.sessionToken = NCMBUser.getSessionToken(); } String sessionToken = mContext.sessionToken; String applicationKey = mContext.applicationKey; String clientKey = mContext.clientKey; NCMBRequest request = new NCMBRequest(scriptUrl, type, content, query, sessionToken, applicationKey, clientKey); URL url = request.getUrl(); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod(request.getMethod()); for (String requestKey : request.getAllRequestProperties().keySet()) { urlConnection.setRequestProperty(requestKey, request.getRequestProperty(requestKey)); } if (header != null && !header.isEmpty()) { for (String requestKey : header.keySet()) { urlConnection.setRequestProperty(requestKey, header.get(requestKey)); } } if (request.getContent() != null) { urlConnection.setDoOutput(true); DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream()); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8")); writer.write(request.getContent()); writer.flush(); writer.close(); } urlConnection.connect(); if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_CREATED || urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); byte[] data = new byte[32768]; int nRead; while ((nRead = urlConnection.getInputStream().read(data, 0, data.length)) != -1) { buffer.write(data, 0, nRead); } responseByte = buffer.toByteArray(); } else { BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getErrorStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line); } br.close(); String statusCode = String.valueOf(urlConnection.getResponseCode()); String message = sb.toString(); if (message.length() > 0 && isJSONString(message)) { JSONObject responseData = new JSONObject(message); if (responseData.has("status")) { statusCode = responseData.getString("status"); } else if (responseData.has("code")) { statusCode = responseData.getString("code"); } if (responseData.has("error")) { message = responseData.getString("error"); } } throw new NCMBException(statusCode, message); } } catch (IOException | JSONException e) { throw new NCMBException(e); } finally { if (urlConnection != null) { urlConnection.disconnect(); } } return responseByte; }
NCMBScriptService extends NCMBService { public byte[] executeScript(String scriptName, MethodType method, Map<String, String> header, JSONObject body, JSONObject query, String baseUrl) throws NCMBException { String scriptUrl; if (baseUrl != null && baseUrl.length() > 0) { scriptUrl = baseUrl + "/" + scriptName; } else { scriptUrl = DEFAULT_SCRIPT_DOMAIN_URL + "/" + DEFAULT_SCRIPT_API_VERSION + "/" + mServicePath + "/" + scriptName; } byte[] responseByte = null; HttpURLConnection urlConnection = null; String type; try { switch (method) { case POST: type = NCMBRequest.HTTP_METHOD_POST; break; case PUT: type = NCMBRequest.HTTP_METHOD_PUT; break; case GET: type = NCMBRequest.HTTP_METHOD_GET; break; case DELETE: type = NCMBRequest.HTTP_METHOD_DELETE; break; default: throw new IllegalArgumentException("Invalid methodType"); } String content = null; if (body != null) { content = body.toString(); } if (mContext.sessionToken == null) { mContext.sessionToken = NCMBUser.getSessionToken(); } String sessionToken = mContext.sessionToken; String applicationKey = mContext.applicationKey; String clientKey = mContext.clientKey; NCMBRequest request = new NCMBRequest(scriptUrl, type, content, query, sessionToken, applicationKey, clientKey); URL url = request.getUrl(); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod(request.getMethod()); for (String requestKey : request.getAllRequestProperties().keySet()) { urlConnection.setRequestProperty(requestKey, request.getRequestProperty(requestKey)); } if (header != null && !header.isEmpty()) { for (String requestKey : header.keySet()) { urlConnection.setRequestProperty(requestKey, header.get(requestKey)); } } if (request.getContent() != null) { urlConnection.setDoOutput(true); DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream()); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8")); writer.write(request.getContent()); writer.flush(); writer.close(); } urlConnection.connect(); if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_CREATED || urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); byte[] data = new byte[32768]; int nRead; while ((nRead = urlConnection.getInputStream().read(data, 0, data.length)) != -1) { buffer.write(data, 0, nRead); } responseByte = buffer.toByteArray(); } else { BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getErrorStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line); } br.close(); String statusCode = String.valueOf(urlConnection.getResponseCode()); String message = sb.toString(); if (message.length() > 0 && isJSONString(message)) { JSONObject responseData = new JSONObject(message); if (responseData.has("status")) { statusCode = responseData.getString("status"); } else if (responseData.has("code")) { statusCode = responseData.getString("code"); } if (responseData.has("error")) { message = responseData.getString("error"); } } throw new NCMBException(statusCode, message); } } catch (IOException | JSONException e) { throw new NCMBException(e); } finally { if (urlConnection != null) { urlConnection.disconnect(); } } return responseByte; } }
NCMBScriptService extends NCMBService { public byte[] executeScript(String scriptName, MethodType method, Map<String, String> header, JSONObject body, JSONObject query, String baseUrl) throws NCMBException { String scriptUrl; if (baseUrl != null && baseUrl.length() > 0) { scriptUrl = baseUrl + "/" + scriptName; } else { scriptUrl = DEFAULT_SCRIPT_DOMAIN_URL + "/" + DEFAULT_SCRIPT_API_VERSION + "/" + mServicePath + "/" + scriptName; } byte[] responseByte = null; HttpURLConnection urlConnection = null; String type; try { switch (method) { case POST: type = NCMBRequest.HTTP_METHOD_POST; break; case PUT: type = NCMBRequest.HTTP_METHOD_PUT; break; case GET: type = NCMBRequest.HTTP_METHOD_GET; break; case DELETE: type = NCMBRequest.HTTP_METHOD_DELETE; break; default: throw new IllegalArgumentException("Invalid methodType"); } String content = null; if (body != null) { content = body.toString(); } if (mContext.sessionToken == null) { mContext.sessionToken = NCMBUser.getSessionToken(); } String sessionToken = mContext.sessionToken; String applicationKey = mContext.applicationKey; String clientKey = mContext.clientKey; NCMBRequest request = new NCMBRequest(scriptUrl, type, content, query, sessionToken, applicationKey, clientKey); URL url = request.getUrl(); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod(request.getMethod()); for (String requestKey : request.getAllRequestProperties().keySet()) { urlConnection.setRequestProperty(requestKey, request.getRequestProperty(requestKey)); } if (header != null && !header.isEmpty()) { for (String requestKey : header.keySet()) { urlConnection.setRequestProperty(requestKey, header.get(requestKey)); } } if (request.getContent() != null) { urlConnection.setDoOutput(true); DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream()); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8")); writer.write(request.getContent()); writer.flush(); writer.close(); } urlConnection.connect(); if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_CREATED || urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); byte[] data = new byte[32768]; int nRead; while ((nRead = urlConnection.getInputStream().read(data, 0, data.length)) != -1) { buffer.write(data, 0, nRead); } responseByte = buffer.toByteArray(); } else { BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getErrorStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line); } br.close(); String statusCode = String.valueOf(urlConnection.getResponseCode()); String message = sb.toString(); if (message.length() > 0 && isJSONString(message)) { JSONObject responseData = new JSONObject(message); if (responseData.has("status")) { statusCode = responseData.getString("status"); } else if (responseData.has("code")) { statusCode = responseData.getString("code"); } if (responseData.has("error")) { message = responseData.getString("error"); } } throw new NCMBException(statusCode, message); } } catch (IOException | JSONException e) { throw new NCMBException(e); } finally { if (urlConnection != null) { urlConnection.disconnect(); } } return responseByte; } NCMBScriptService(NCMBContext context); }
NCMBScriptService extends NCMBService { public byte[] executeScript(String scriptName, MethodType method, Map<String, String> header, JSONObject body, JSONObject query, String baseUrl) throws NCMBException { String scriptUrl; if (baseUrl != null && baseUrl.length() > 0) { scriptUrl = baseUrl + "/" + scriptName; } else { scriptUrl = DEFAULT_SCRIPT_DOMAIN_URL + "/" + DEFAULT_SCRIPT_API_VERSION + "/" + mServicePath + "/" + scriptName; } byte[] responseByte = null; HttpURLConnection urlConnection = null; String type; try { switch (method) { case POST: type = NCMBRequest.HTTP_METHOD_POST; break; case PUT: type = NCMBRequest.HTTP_METHOD_PUT; break; case GET: type = NCMBRequest.HTTP_METHOD_GET; break; case DELETE: type = NCMBRequest.HTTP_METHOD_DELETE; break; default: throw new IllegalArgumentException("Invalid methodType"); } String content = null; if (body != null) { content = body.toString(); } if (mContext.sessionToken == null) { mContext.sessionToken = NCMBUser.getSessionToken(); } String sessionToken = mContext.sessionToken; String applicationKey = mContext.applicationKey; String clientKey = mContext.clientKey; NCMBRequest request = new NCMBRequest(scriptUrl, type, content, query, sessionToken, applicationKey, clientKey); URL url = request.getUrl(); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod(request.getMethod()); for (String requestKey : request.getAllRequestProperties().keySet()) { urlConnection.setRequestProperty(requestKey, request.getRequestProperty(requestKey)); } if (header != null && !header.isEmpty()) { for (String requestKey : header.keySet()) { urlConnection.setRequestProperty(requestKey, header.get(requestKey)); } } if (request.getContent() != null) { urlConnection.setDoOutput(true); DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream()); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8")); writer.write(request.getContent()); writer.flush(); writer.close(); } urlConnection.connect(); if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_CREATED || urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); byte[] data = new byte[32768]; int nRead; while ((nRead = urlConnection.getInputStream().read(data, 0, data.length)) != -1) { buffer.write(data, 0, nRead); } responseByte = buffer.toByteArray(); } else { BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getErrorStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line); } br.close(); String statusCode = String.valueOf(urlConnection.getResponseCode()); String message = sb.toString(); if (message.length() > 0 && isJSONString(message)) { JSONObject responseData = new JSONObject(message); if (responseData.has("status")) { statusCode = responseData.getString("status"); } else if (responseData.has("code")) { statusCode = responseData.getString("code"); } if (responseData.has("error")) { message = responseData.getString("error"); } } throw new NCMBException(statusCode, message); } } catch (IOException | JSONException e) { throw new NCMBException(e); } finally { if (urlConnection != null) { urlConnection.disconnect(); } } return responseByte; } NCMBScriptService(NCMBContext context); byte[] executeScript(String scriptName, MethodType method, Map<String, String> header, JSONObject body, JSONObject query, String baseUrl); void executeScriptInBackground(final String scriptName, final MethodType method, final Map<String, String> header, final JSONObject body, final JSONObject query, final String baseUrl, final ExecuteScriptCallback callback); }
NCMBScriptService extends NCMBService { public byte[] executeScript(String scriptName, MethodType method, Map<String, String> header, JSONObject body, JSONObject query, String baseUrl) throws NCMBException { String scriptUrl; if (baseUrl != null && baseUrl.length() > 0) { scriptUrl = baseUrl + "/" + scriptName; } else { scriptUrl = DEFAULT_SCRIPT_DOMAIN_URL + "/" + DEFAULT_SCRIPT_API_VERSION + "/" + mServicePath + "/" + scriptName; } byte[] responseByte = null; HttpURLConnection urlConnection = null; String type; try { switch (method) { case POST: type = NCMBRequest.HTTP_METHOD_POST; break; case PUT: type = NCMBRequest.HTTP_METHOD_PUT; break; case GET: type = NCMBRequest.HTTP_METHOD_GET; break; case DELETE: type = NCMBRequest.HTTP_METHOD_DELETE; break; default: throw new IllegalArgumentException("Invalid methodType"); } String content = null; if (body != null) { content = body.toString(); } if (mContext.sessionToken == null) { mContext.sessionToken = NCMBUser.getSessionToken(); } String sessionToken = mContext.sessionToken; String applicationKey = mContext.applicationKey; String clientKey = mContext.clientKey; NCMBRequest request = new NCMBRequest(scriptUrl, type, content, query, sessionToken, applicationKey, clientKey); URL url = request.getUrl(); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod(request.getMethod()); for (String requestKey : request.getAllRequestProperties().keySet()) { urlConnection.setRequestProperty(requestKey, request.getRequestProperty(requestKey)); } if (header != null && !header.isEmpty()) { for (String requestKey : header.keySet()) { urlConnection.setRequestProperty(requestKey, header.get(requestKey)); } } if (request.getContent() != null) { urlConnection.setDoOutput(true); DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream()); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8")); writer.write(request.getContent()); writer.flush(); writer.close(); } urlConnection.connect(); if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_CREATED || urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); byte[] data = new byte[32768]; int nRead; while ((nRead = urlConnection.getInputStream().read(data, 0, data.length)) != -1) { buffer.write(data, 0, nRead); } responseByte = buffer.toByteArray(); } else { BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getErrorStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line); } br.close(); String statusCode = String.valueOf(urlConnection.getResponseCode()); String message = sb.toString(); if (message.length() > 0 && isJSONString(message)) { JSONObject responseData = new JSONObject(message); if (responseData.has("status")) { statusCode = responseData.getString("status"); } else if (responseData.has("code")) { statusCode = responseData.getString("code"); } if (responseData.has("error")) { message = responseData.getString("error"); } } throw new NCMBException(statusCode, message); } } catch (IOException | JSONException e) { throw new NCMBException(e); } finally { if (urlConnection != null) { urlConnection.disconnect(); } } return responseByte; } NCMBScriptService(NCMBContext context); byte[] executeScript(String scriptName, MethodType method, Map<String, String> header, JSONObject body, JSONObject query, String baseUrl); void executeScriptInBackground(final String scriptName, final MethodType method, final Map<String, String> header, final JSONObject body, final JSONObject query, final String baseUrl, final ExecuteScriptCallback callback); static final String SERVICE_PATH; static final String DEFAULT_SCRIPT_DOMAIN_URL; static final String DEFAULT_SCRIPT_API_VERSION; }