blob: 92df6201ba0a67bd74491335a28ebd585a844f0b [file] [log] [blame]
/*
* Copyright (c) 2020 Kentyou.
* 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
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Kentyou - initial API and implementation
*/
package org.eclipse.sensinact.gateway.nthbnd.rest.http.test;
import org.eclipse.sensinact.gateway.common.bundle.Mediator;
import org.eclipse.sensinact.gateway.protocol.http.client.ConnectionConfigurationImpl;
import org.eclipse.sensinact.gateway.protocol.http.client.SimpleRequest;
import org.eclipse.sensinact.gateway.protocol.http.client.SimpleResponse;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.net.ConnectException;
//import org.eclipse.sensinact.gateway.util.crypto.Base64;
public class HttpServiceTestClient {
public static String newRequest(Mediator mediator, String url, String content, String method) {
SimpleResponse response;
ConnectionConfigurationImpl<SimpleResponse, SimpleRequest> builder =
new ConnectionConfigurationImpl<SimpleResponse, SimpleRequest>();
builder.setUri(url);
builder.setAccept("application/json");
builder.setConnectTimeout(60000);
builder.setReadTimeout(60000);
/*builder.addHeader("Authorization",
"Basic " + Base64.encodeBytes(
"cea:sensiNact_team".getBytes()));*/
try {
if (method.equals("GET")) {
builder.setHttpMethod("GET");
} else if (method.equals("POST")) {
builder.setContentType("application/json");
builder.setHttpMethod("POST");
if (content != null && content.length() > 0) {
JSONObject jsonData = new JSONObject(content);
builder.setContent(jsonData.toString());
}
} else {
return null;
}
SimpleRequest request = new SimpleRequest(builder);
response = request.send();
//System.out.println(response.getHeaders());
byte[] responseContent = response.getContent();
String contentStr = (responseContent == null ? null : new String(responseContent));
return contentStr;
}catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}