Http Get request is not working - scala

I am trying to get xml data from apache ranger using it's rest api. Here is my code
val httpclient = new DefaultHttpClient()
val auth=new AuthScope(host,AuthScope.ANY_PORT)
val credentials=new UsernamePasswordCredentials(username, password)
httpclient.getCredentialsProvider()
.setCredentials(auth, credentials)
val httpget = new HttpGet("http://localhost:6080/service/public/api/repository/1")
httpget.setHeader("Accept", "application/xml")
val response = httpclient.execute(httpget)
val entity = response.getEntity
if (entity != null) {
val in = new BufferedReader(new InputStreamReader(entity.getContent()))
var line = in.readLine()
var response = new StringBuffer()
while (line != null) {
response.append(line)
line = in.readLine()
}
in.close()
println(response.toString())
}
If i hit this url from browser it shows result fine. But in case of code it returns html.
Any help?

Try this code
val httpclient = new DefaultHttpClient()
val httpget = new HttpGet("url")
httpget.addHeader(BasicScheme.authenticate(
new UsernamePasswordCredentials("user", "pass"),
"UTF-8", false))
httpget.setHeader("Accept", "application/json")
val response = httpclient.execute(httpget)
EntityUtils.toString(response.getEntity())
setting authentication in the request.
Thanks

Related

How to send POST request to another microservice containing enum #RequestParam in kotlin?

I tried to send request to another service containing enum #RequestParam but it always fails.
Here's the example of my request;
fun upsertExclusionOverride(
request: request
): ExcOve? {
val builder = UriComponentsBuilder.fromUriString("/v1/p-b/e/bulk")
val httpEntity = RestTemplateUtils.getHttpEntityCustomHeaders(request, headers)
try {
val body = restTemplate
.exchange(
builder.toUriString(),
HttpMethod.POST,
httpEntity,
Response::class.java
)
.body
?: throw Exception("Fail")
return body.toDomain()
} catch (e: RestClientException) {
log.error(e.message)
throw Exception("Fail")
}
}
This is the other microservice;
#PostMapping("/e/bulk")
#ApiOperation("Exclude")
fun exclusionsInBulk(
#RequestParam(name = "operation", required = true) operation: Operation,
#RequestPart("file") #ApiParam(
value = "File",
required = true,
format = "byte"
) file: MultipartFile
): ResponseEntity<Response> {
.....
}
How should I prevent 400 Bad Request?
I added enum converter but it didn't work.
I expect it to not to get 400 Bad Request.

How to call PUT on Moodle with PutAsync?

I am trying to modernize some calls to Moodle REST APIs.
Everything is doing OK as long as I use querystring, but I can't pinpoint why using a PutAsync doesn't work in this case (tested with Postman, it works):
using (var httpClient = _httpClientFactory.CreateClient())
{
var url = "http://127.0.0.1/moodle/webservice/rest/server.php";
var urlFunction = UrlFunction.core_user_create_users.ToString();
var urlFormat = UrlFormat.json.ToString();
var requestModel = new GetCoursesRequestModel()
{
wstoken = [token],
wsfunction = urlFunction,
moodlewsrestformat = urlFormat
};
var jsonRequest = JsonConvert.SerializeObject(requestModel);
var stringContentRequest = new StringContent(jsonRequest, Encoding.UTF8, "application/json");
var response = await httpClient.PutAsync(url, stringContentRequest);
response.EnsureSuccessStatusCode();
var courseDtos = await response.Content.ReadFromJsonAsync<List<MoodleAPIDtosCourseDto>>();
if (courseDtos is null)
throw new InputFormatterException(
$"Pb with response format.");
}
ยดยดยดยด
The error is generated when I try to read my response at await response.Content.ReadFromJsonAsync()

Synchronizing clients and server

I'm making a distributed sorting system, where each machine (client) has a specific range of values (partition) to store. I don't have any problem sending and receiving data, but the synchronization is my problem. Before retrieving the wanted partition from the server, it must first arrive to the server from one of the other clients.
I've tried with delay and various other RPC calls to try to try to synchronize the clients and server, but the clients run out of synchronization, and the program breaks. I think I have to incorporate future or promise, but I'm not sure how.
Every client has a unique ID (from 0 and up), and will store partitions according to that number. One line of data looks like this: AsfAGHM5om 00000000000000000000000000000000 0000222200002222000022220000222200002222000000001111
EDIT: updated code for example
Client
val numberOfClients = 3
var sentPartitionsCounter = 0
while(sentPartitionsCounter < numberOfClients) {
if(sentPartitionsCounter != id){
sendUnwantedPartition("src/main/scala/testFile."+sentPartitionsCounter.toString) //"partition."+client.sentPartitionsCounter
sentPartitionsCounter += 1
}
client.getWantedPartition()
}
def sendUnwantedPartition(filename: String): Unit = {
val dataList = Source.fromFile(filename).getLines.toList
val dataSeq = for {
dataLine <- dataList
dataValues = dataLine.split(" ", 2)
} yield (Data(key = dataValues(0), value = dataValues(1)))
val partitionID = filename takeRight 1
val request = Dataset(data = dataSeq, partitionID = partitionID.toInt)
val response = blockingStub.getUnwantedPartitions(request)
print("sent partitions")
}
def getWantedPartition(): Unit = {
val request = ID(id = id)
val response = blockingStub.sendWantedPartitions(request)
val wantedPartitions = new File("clientFiles/wantedPartitions"+id.toString+".txt")
val printWriter: PrintWriter = new PrintWriter(new FileWriter(wantedPartitions, true));
if(!wantedPartitions.exists()){
wantedPartitions.createNewFile()
}
for {
data <- response.data
} yield (printWriter.append(data.key + " " + data.value + "\n"))
printWriter.close();
}
Server
override def getUnwantedPartitions(req: Dataset) = {
val filename = "serverPartitions/partition"+req.partitionID+".txt"
val partition = new File(filename)
val printWriter: PrintWriter = new PrintWriter(new FileWriter(partition, true));
if(!partition.exists()){
partition.createNewFile()
}
for {
data <- req.data
} yield (printWriter.append(data.key + " " + data.value + "\n"))
printWriter.close();
Future.successful(Text("Got unwanted data"))
}
override def sendWantedPartitions(req: ID) = {
val filename = "serverPartitions/partition"+req.id+".txt"
try {
val dataList = Source.fromFile(filename).getLines.toList
val dataSeq = for {
dataLine <- dataList
dataValues = dataLine.split(" ", 2)
} yield (Data(key = dataValues(0), value = dataValues(1)))
val reply = Dataset(data = dataSeq, partitionID = req.id)
new File(filename).delete()
Future.successful(reply)
} catch {
// Partition hasn't been received yet
case e: FileNotFoundException => Future.successful(Dataset())
}
}
Proto
syntax = "proto3";
package protoPackage;
service DistrSorting {
rpc getUnwantedPartitions(Dataset) returns (Text) {}
rpc sendWantedPartitions(ID) returns (Dataset) {}
}
message ID {
int32 id = 1;
}
message Text {
string text = 1;
}
message Dataset {
repeated Data data = 1;
int32 partitionID = 2;
}
message Data {
string key = 1;
string value = 2;
}

Unable to get token from tableau server

I am following below link to integrate tableau report on web application(asp .net mvc),
https://onlinehelp.tableau.com/current/server/en-us/trusted_auth.htm
https://onlinehelp.tableau.com/current/server/en-us/trusted_auth_webrequ.htm
Code :
var uri = "http://<server ip>:8000/trusted";
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("username ", "<user>");
parameters.Add("target_site", "<site>");
var bodyParameters = new ArrayList();
foreach (var parameter in parameters)
{
bodyParameters.Add(string.Format("{0}={1}", HttpUtility.UrlEncode(parameter.Key), HttpUtility.UrlEncode(Convert.ToString(parameter.Value))));
}
string requestBody = String.Join("&", bodyParameters.ToArray());
var request = WebRequest.CreateHttp(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
try
{
using (var writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(requestBody);
}
var response = (HttpWebResponse)request.GetResponse();
using (var reader = new StreamReader(response.GetResponseStream()))
{
String body = reader.ReadToEnd();
}
}
catch (Exception ex)
{
string str = ex.Message.ToString();
}
My machine is registered as trusted host on tableau server, still i am getting -1 when i request for token.

How to generate swagger document for akka http web socket route?

How to generate swagger document for akka http web socket route ?
I have able to write akka Http get,put,post,delete. for example
#Path("/postPing")
#ApiOperation(value = "Find a ping", notes = "Returns a pong",
httpMethod = "POST",response = classOf[String])
#ApiImplicitParams(Array(
new ApiImplicitParam(name = "data", value = "\"data\" to sum", required = true,
dataType = "string", paramType = "query"),
new ApiImplicitParam(name = "file", required = true,
dataType = "file", paramType = "query")
))
#ApiResponses(Array(
new ApiResponse(code = 404, message = "websocket not found"),
new ApiResponse(code = 200, message = "websocket found"),
new ApiResponse(code = 400, message = "Invalid websocket supplied")))
def postRoute = path("postPing") {
complete("post pong")
}
But I need for Akka web socket
def webSocketRoute: Route = path("websocket") {
handleWebSocketMessages(broadcast)
}
def broadcast: Flow[Message, Message, Any] = {
Flow[Message].mapConcat {
case tm: TextMessage =>
TextMessage(tm.textStream) :: Nil
}
}
For example
To connect with websocket server
/connect ws://echo.websocket.org/websocket
To send data to websocket server
/send Hello\ world
Thanks in Advance