Sisense - REST API - republish Not Working + 500 Internal Server Error - json

Using JAVA, I am trying to republish a dashboard to a particular User. It returns me HTTP status 500. Below is the code for it.
String sisenseURL = surl; // This is correct URL to POST API for PUBLISH
String urlParameters = "force=true";
byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 );
int postDataLength = postData.length;
URL url = new URL( sisenseURL );
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setInstanceFollowRedirects(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Content-Length", Integer.toString(postDataLength ));
conn.setRequestProperty("Authorization", accessToken);
conn.setUseCaches(false);
try(DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
wr.write( postData );
}
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
final StringBuffer stringBuffer = new StringBuffer();
String line;
while ((line = in.readLine()) != null) {
stringBuffer.append(line);
}
in.close();
The request runs file with POSTMAN as well as with the Swagger UI for Sisense.
Any help would be greatly appreciated.
TIA
Ashutosh

Here is a java example for sisense V6.7 of updating dashboard shares using the rest API
You didnt share your payload so not sure if thats the problem, but the example below worked for me.
I took the sendPostRequest code from here
import java.io.*;
import java.net.*;
public class Runner {
public static void main(String[] args){
try {
//Dashboard shares payload
String payload = "{\"sharesTo\":[{\"shareId\":\"58504c5221785b627cb4361d\",\"type\":\"user\",\"subscribe\":false},{\"shareId\":\"58505ba6ec4df9701a000019\",\"type\":\"user\",\"rule\":\"view\",\"subscribe\":false}]}";
String str = sendPostRequest(getDashboardUrl(), payload);
System.out.println("Done");
}
catch (RuntimeException e){
}
}
public static String sendPostRequest(String requestUrl, String payload) {
try {
URL url = new URL(requestUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
connection.setRequestProperty("Authorization", getAuthorization());
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
writer.write(payload);
writer.close();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuffer jsonString = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
jsonString.append(line);
}
br.close();
connection.disconnect();
return jsonString.toString();
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
public static String getDashboardUrl(){
//Sisense domain
String baseURL = "http://localhost:8081";
return baseURL + "/api/shares/dashboard/5850511cec4df9701a000013";
}
public static String getAuthorization(){
return "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjoiNTg1MDRjNTIyMTc4NWI2MjdjYjQzNjFkIiwiYXBpU2VjcmV0IjoiOGUwZDIyOWItY2VmMS0xYTE4LTNhYWEtYmY1ZmE1ZmNkNTExIiwiaWF0IjoxNTE1MDEzMzkxfQ.zgx0Nv8YztfM2rm5WTCnJ0R6C_n5V-HNkEZgAcINfs4";
}
}

Related

How to send data from libgdx project to web?

I would like to work on moving the json data from libgdx to my web server, but I am not sure how to do it. The method below was created by referring to libgdx's documentation.
private void httpPostJson(){
final Json json = new Json();
final String requestJson = json.toJson(requestObject);
Net.HttpRequest request = new Net.HttpRequest("POST");
final String url = "http://localhost:8080/data";
request.setUrl(url);
request.setContent(requestJson);
request.setHeader("Content-Type", "application/json");
Gdx.net.sendHttpRequest(request, new Net.HttpResponseListener() {
#Override
public void handleHttpResponse(Net.HttpResponse httpResponse) {
String responseJson = httpResponse.getResultAsString();
Gson gson = new Gson();
data = gson.fromJson(responseJson, Person.class);
//'Person' is just sample class. data is class Person's object.
data.StoreData("",1);//successed to receive json data from web server.
//StoreData is just getter method.
}
#Override
public void failed(Throwable t) {
Gdx.app.log("failed!");
}
#Override
public void cancelled() {
Gdx.app.log("cancelled!");
}
});
}
It is possible to receive data transmitted from a web server.
But, this method can't send data to web server.
Can you tell me how to move data from libgdx project to web server?
This is the data transmitted to the web server:
final String requestJson = json.toJson(requestObject);
We are using the following Code (as you have more control over the request as opposed to using gdx.net), works like a charm, just don't execute on the main thread - body is your JSON as String
URL url = new URL(<your url>);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Content-Type",
"application/json; charset=utf-8");
if (body != null) {
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
os, "UTF-8"));
writer.write(body);
writer.close();
os.close();
}
conn.connect();
String s = stringFromStream(conn.getInputStream(), 4096);
Method stringFromStream:
public static String stringFromStream(final InputStream is,
final int bufferSize) {
final char[] buffer = new char[bufferSize];
final StringBuilder out = new StringBuilder();
try {
final Reader in = new InputStreamReader(is, "UTF-8");
try {
for (; ; ) {
int rsz = in.read(buffer, 0, buffer.length);
if (rsz < 0)
break;
out.append(buffer, 0, rsz);
}
} finally {
in.close();
}
} catch (Exception ex) {
}
return out.toString();
}

decompress Apache httpclient results

I'm using Apache HTTPClient 4.5.3 to make some HTTP requests, but I am getting a g-zipped response I have tried many things I found online but non of them worked. I still get gibberish when I print the response. Below are the relevant code. What do I need to do to get a human readable response?
static public CloseableHttpClient CreateHttpClient() {
// return
// HttpClients.custom().disableAutomaticRetries().setHttpProcessor(HttpProcessorBuilder.create().build())
// .build();
return HttpClientBuilder.create().disableAutomaticRetries()
.setHttpProcessor(HttpProcessorBuilder.create().build()).build();
}
static public RequestConfig GetConfig() {
return RequestConfig.custom().setSocketTimeout(READTIMEOUT).setConnectTimeout(CONNECTTIMEOUT)
.setConnectionRequestTimeout(REQUESTTIMEOUT).build();
}
static public String updates() {
String result = "";
String url = "https://example.com";
CloseableHttpClient httpClient = CreateHttpClient();
CloseableHttpResponse response = null;
URL urlObj;
RequestConfig config = GetConfig();
try {
urlObj = new URL(url);
HttpPost request = new HttpPost(url);
request.setConfig(config);
StringEntity params = new StringEntity("example");
request.addHeader("Accept-Language", "en");
request.addHeader("Content-Type", "application/json; charset=UTF-8");
request.addHeader("Content-Length", String.valueOf(params.getContentLength()));
request.addHeader("Host", urlObj.getHost());
request.addHeader("Connection", "Keep-Alive");
request.addHeader("Accept-Encoding", "gzip");
request.setEntity(params);
response = httpClient.execute(request);
int responseCode = response.getStatusLine().getStatusCode();
System.out.println("updates response code: " + responseCode);
// BufferedReader rd = new BufferedReader(new
// InputStreamReader(response.getEntity().getContent(), "UTF-8"));
result = EntityUtils.toString(response.getEntity());
// String line = "";
// while ((line = rd.readLine()) != null) {
// result.append(line);
// }
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null)
response.close();
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
You should get the content from the response entity which is an InputStream. Than you could create a String from that InputStream with BufferedReader
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
String result = convertInputStreamToString(instream);
instream.close();
}
Write your own convertInputStreamToString. If you need help for doing that check here:
Read/convert an InputStream to a String

Get HTML Source in Android Studio

I test this function, but don't work
Permission is ok into manifest file
What's I doing wrong?
public void getHTML() throws Exception {
URL url = new URL("http://www.android.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(urlConnection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = br.readLine()) != null) {
response.append(inputLine);
}
txtInfo.setText("123");
br.close();
}finally {
urlConnection.disconnect();
}
}
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
You could also use AsynTask

Use URLConnection instead of HttpClient in android studio

Is there any way to convert this code into URLCOnnection instead of using Httpclient ? Im using API23 and HTTP CLient is not supported anymore. I have a hard time using URLCOnnection because almost all the tutorials uses HttpClient for connection. thanks
public class SigninActivity extends AsyncTask<String,Void,String>{
private TextView statusField,roleField;
private Context context;
private int byGetOrPost = 0;
//flag 0 means get and 1 means post.(By default it is get.)
public SigninActivity(Context context,TextView statusField,TextView roleField,int flag) {
this.context = context;
this.statusField = statusField;
this.roleField = roleField;
byGetOrPost = flag;
}
protected void onPreExecute(){
}
#Override
protected String doInBackground(String... arg0) {
if(byGetOrPost == 0){ //means by Get Method
try{
String username = (String)arg0[0];
String password = (String)arg0[1];
String link = "http://myphpmysqlweb.hostei.com/login.php?username="+username+"& password="+password;
URL url = new URL(link);
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(link));
HttpResponse response = client.execute(request);
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line="";
while ((line = in.readLine()) != null) {
sb.append(line);
break;
}
in.close();
return sb.toString();
}
catch(Exception e){
return new String("Exception: " + e.getMessage());
}
}
else{
try{
String username = (String)arg0[0];
String password = (String)arg0[1];
String link="http://myphpmysqlweb.hostei.com/loginpost.php";
String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8");
data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
URL url = new URL(link);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write( data );
wr.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
{
sb.append(line);
break;
}
return sb.toString();
}
catch(Exception e){
return new String("Exception: " + e.getMessage());
}
}
}
#Override
protected void onPostExecute(String result){
this.statusField.setText("Login Successful");
this.roleField.setText(result);
}
}
This is the basic format for URLConnection that I merged from some working code of mine with yours. If you fill in your code where I've specified, it should work.
//-------------------
String username = (String)arg0[0];
String password = (String)arg0[1];
String link = "http://myphpmysqlweb.hostei.com/login.php?username="+username+"& password="+password;
InputStream is = null;
try {
URL url = new URL(link);
URLConnection urlConnection = (HttpURLConnection) url.openConnection();
is = urlConnection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
//FILL IN YOUR CODE HERE!
//FILL IN YOUR CODE HERE!
} catch(Exception e){
return new String("Exception: " + e.getMessage());
}
//-------------------

How do I upgrade from DefaultHttpClient() to HttpClientBuilder.create().build()?

I have a routine which checks if a record has been indexed by Solr. I have a deprecated method of creating a HTTPClient which I'm trying to remove:
From
DefaultHttpClient httpClient = new DefaultHttpClient();
To
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
The problem I now have is that after 2 call to the URL, the 3rd attempt seems to hang. I'm not quite sure what I'm missing if anyone can help please?
This is my complete method which I've extracted out into a test:
#Test
public void checkUntilRecordAvailable() {
String output;
String solrSingleJobURL = "http://solr01.prod.efinancialcareers.com:8080/solr/jobSearchCollection/select?q=id%3A7618769%0A&fl=*&wt=json&indent=true";
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet(solrSingleJobURL);
StringBuilder jobResponseBuilder;
Gson gson = new Gson();
while (true) {
System.out.print("WAITING FOR SOLR PARTIAL TO RUN " + solrSingleJobURL);
jobResponseBuilder = new StringBuilder();
try {
HttpResponse response = httpClient.execute(httpGet);
BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
while ((output = br.readLine()) != null) {
System.out.println(output);
jobResponseBuilder.append(output);
}
JobResponse jobResponse = gson.fromJson(jobResponseBuilder.toString(), JobResponse.class);
Long numberOfRecordsFound = jobResponse.getNumberOfRecordsFound();
if (numberOfRecordsFound == 0) {
System.out.println("- PAUSE FOR 10 SECONDS UNTIL NEXT CHECK");
Thread.sleep(5000);
} else {
System.out.println(" RECORD FOUND ");
httpClient.close();
break;
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
here is some code using the builders from 4.3 httpclient.. dont know if it helps .. I use skeleton from here. So , i wrap the creation of the httpclient in a runnable and post it to a que-processor for the EXEC. Note the runnable has your 'builder' stuff in it.
RequestConfig config = null;
private HttpClientContext context;
public void create(int method, final String url, final String data) {
this.method = method;// GET, POST, HEAD, DELETE etc
this.url = url;
this.data = data; //entity body of POST
this.config = RequestConfig.custom()
.setConnectionRequestTimeout(60 * 1000)
.setSocketTimeout(60 * 1000)
.build();
this.context = HttpClientContext.create();
ConnectionMgr.getInstance().push(this);
}
//above creates a runnable that can be posted to a generic execution que
//detls on run() include builder asked about
public void run() {
handler.sendMessage(Message.obtain(handler, HttpConnection.DID_START));
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(YourConnectionMgr.getInstance())
.addInterceptorLast(new HttpRequestInterceptor() {
public void process(
final HttpRequest request,
final HttpContext context) throws HttpException, IOException {
if (request.getRequestLine().getMethod() == "POST"){
request.addHeader("Content-Type", mimeType) ;}
}else if(request.getRequestLine().getMethod() == "GET" && !request.getRequestLine().getUri().toString().contains("ffmpeg")){
request.addHeader("X-Parse-Application-Id", ParseApplication.key_appId);
}
}) .build();