Exist-db parallel request(bulk request) failed on exist db restxq (post,put) - exist-db

I am facing a problem on restxq multiple parallel request of post, put. When request from web application as async bulk request (approx. more then 5 ).
I am using exist db 5.1.0 or 5.1.1
I am sending request through angular forkjoin because of i want to submit more then 20 or 50 request in parallel
declare
%rest:path("/docs/{$doc-id}")
%rest:PUT("{$body}")
%rest:consumes("application/json")
%output:method("json")
function doc-rx:update-docs($doc-id as xs:string, $body) {
try{
let $request:= parse-json(util:binary-to-string( $body))
return
<result>Success</result>
}catch * {
<error>Caught error {$err:description}</error>
}
};
Some request will failed with below message, some request got cancelled
Failed Message:
The underlying InputStream has been closed. Unable to encode string value: The underlying InputStream has been closed Some Request got cancelled.
Please advise how can I resolve this issue.

Related

Exist-db Multiple request(post,put) failed on restxq

I am facing a problem on restxq multiple parallel request of post, put. When request from web application as async bulk request (approx. more then 5 ).
declare
%rest:path("/docs/{$doc-id}")
%rest:PUT("{$body}")
%rest:consumes("application/json")
%output:method("json")
function doc-rx:update-docs($doc-id as xs:string, $body) {
try{
let $request:= parse-json(util:binary-to-string( $body))
return
<result>Success</result>
}catch * {
<error>Caught error {$err:description}</error>
}
};
Some request will failed with below message, some request got cancelled
Failed Message:
The underlying InputStream has been closed. Unable to encode string value: The underlying InputStream has been closed Some Request got cancelled.
Please advise how can I resolve this issue.

Google places API returning failed to open stream: HTTP request failed error

I am hoping for some direction on this error. I have a loop that connects with the google places API. Every few times I test it I get an HTTP request failed error. But when I take the API call from the error it works in a browser or in postman. Any suggestions for what might be causing this? Thanks for all of your help. Here is my API call and the error.
$googleApiPlaceUri = 'https://maps.googleapis.com/maps/api/place/textsearch/json';
foreach ($attractions as $attraction) {
$api_call = "{$googleApiPlaceUri}?query={$attraction}+in+{$city}&key={$api_key}";
$api_return_array[] = json_decode(file_get_contents($api_call));
foreach ($api_return_array as $result) {
$results = $result->results;
array_push($results_array, $results);
}
}
Here is the error I get...
Warning: file_get_contents(https://maps.googleapis.com/maps/api/place/textsearch/json?query=hiking +in+Denver&key=API_KEY): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request

Enable authenticator manually

Currently my client authenticates request only on case of 401 response:
this.client.authenticator(new okhttp3.Authenticator() {
public Request authenticate(Route route, Response response) throws IOException {
String credentials = authenticator.getCredentials();
if (credentials.equals(response.request().header("Authorization"))) {
throw new TraversonException(401, "Unauthorized", response.request().url().toString());
} else {
defaultHeader("Authorization", credentials);
Request.Builder newRequest = response.request().newBuilder()
.headers(Headers.of(defaultHeaders));
return newRequest.build();
}
});
But I'd like to change this behavior and be able to call it either manually or auto per first call? Is it possible somehow?
If the authentication is predictably required and not related to a proxy, then the solution is to implement an Interceptor instead of Authenticator.
OkHttpClient.Builder clientBuilder = ...;
clientBuilder.networkInterceptors().add(0, myInterceptor);
client = clientBuilder.build();
Example Interceptor https://github.com/yschimke/oksocial/blob/48e0ca53b85e608443eab614829cb0361c79aa47/src/main/java/com/baulsupp/oksocial/uber/UberAuthInterceptor.java
n.b. There is discussion around possible support for this usecase in https://github.com/square/okhttp/pull/2458. One issue with current Authenticator API is that it assumes a Response from the failed (401) request.

Google Custom Search API error

I used Google Cloud Platform for Free trial
https://cloud.google.com/free-trial/
to use custom search API in my java application
I write the following code in my application
URL url = new URL("https://www.googleapis.com/customsearch/v1?key="+key+
"&cx=013036536707430787589:_pqjad5hr1a&q="+ qry + "&alt=json");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
if(output.contains("\"link\": \"")){
String link=output.substring(output.indexOf("\"link\": \"")+("\"link\": \"").length(), output.indexOf("\","));
System.out.println(link); //Will print the google search links
}
}
conn.disconnect();
it some times gave me a result and after that it gave me the following error , and this error occurred regardless the query string
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: https://www.googleapis.com/customsearch/v1?key=????&cx=013036536707430787589:_pqjad5hr1a&q=?????
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1839)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1440)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
Where is the problem please?
403 is specifically Google's forbidden error - it means your sending too many requests too quickly and they think you using a bot or automation
If you load that query in your browser and watch the requests - keep hitting refresh quickly until you see an html response that says
We're sorry...
... but your computer or network may be sending automated queries. To protect our users, we can't process your request right now.
See Google Help for more information.

Status of ajax or post request

status - contains the status of the request like
("success", "notmodified", "error", "timeout", or "parsererror") /ajax or post
I know what is success,not modified,error meant here but I am unable to find out how to handle this errors. If the call back is success then update my div, if there is any error not modified, error, time out, parse error then let me alert a pop up some error occurred.
What would be the cause for each type of error? I mean the situations where not modified,timeout,error and parseerror occurs.
If it results success, then does it mean my post request has successfully worked?
My local-server xampp never results any error, the status is always success. I guess, since its limited to my system but when I put my website online there exists several issues like traffic on server.
So how do I find out, whether my post request to some sample.php page was successfully sent and else pop out an alert to user if something went wrong?
The error types are a little self-explanatory. They simply provide a string for you to easily handle the different errors.
error callback option is invoked, if the request fails. It receives the jqXHR, a string indicating the error type, and an exception object if applicable. Some built-in errors will provide a string as the exception object: "abort", "timeout", "No Transport".
Source: jQuery.Ajax documentation
Codes Explained:
Error: Any of the HTTP response codes, like the well-know 404 (not found) or other internal server errors.
Notmodified: Compares the cached version of the browser with the server's version. If they are the same, the server responds with a 304
Timeout: Ajax requests are time-limited, so errors can be caught and handled to provide a better user experience. Request timeouts are usually either left at their default or set as a global default using $.ajaxSetup() rather than being overridden for specific requests with the timeout option.
Parse Error: The jQuery data (JSON) cannot be parsed (usually due to syntax errors)
Handling these error codes:
Here is some example of handling the errors
$(function() {
$.ajaxSetup({
error: function(jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}
}
});
});
Source: Blog Post - jQuery Error Handling
Success
Response code is between 200-299 or is 304 Not Modified.
Not Modified
Response code is 304. If you employ caching, the browser can tell the server which version it currently has, and the server compares this with its version and if there has been no change, it can send a 304 Not Modified response, to indicate to the client that their version is up to date. In jQuery ajax, a 304 Not Modified response will still fire the success handler.
Error
Response code is between 400-599. This could be for example 404 not found, 403 forbidden, 500 internal server error etc.
Parse Error
This is a jQuery internal, not actually a HTTP response. This will happen if jQuery is trying to parse JSON or XML that is not in the valid format or has syntax errors.
Timeout
Again, this isn't a HTTP response. The ajax request has a timeout which if is exceeded before the server responds, will abort the request.
If you control the server side, in your example a PHP script, and you never change the response code using header() then your ajax will always receive 200 OK responses unless there is an unhandled exception in the PHP which will trigger a 500 internal server error.
It is acceptable to always send 200 OK response codes. For example, if the server outputs a JSON object which contains its own success/error flag then any errors can be handled by looking at the flag.
As far as I know
not modified: Server sends a Not Modified(304) response status
timeout: the server has not responded within the time period specified by the timeout property
error: server response with a error status like 4xx or 5xx
parseerror: there was an client side error when processing server response like an invalid json format/xml format

Resources