how to convert blob to arraybuffer or string to blob - java

Imagine I have a file with this data:
lastModified: 1543877513859
lastModifiedDate: Tue Dec 04 2018 02:21:53 GMT+0330 (Iran Standard Time) {}
name: "ali.jpg"
objectURL: SafeUrlImpl
changingThisBreaksApplicationSecurity: "blob:http://localhost:4200/b45412e9-69ad-433f-b24d-b3833d705da4"
__proto__: SafeValueImpl
size: 16196
type: "image/jpeg"
webkitRelativePath: ""
So I want to get the binary data of this image. I read some info of how to get the content of image here. so I try to get the binary data with this:
const expecteadBinaryData= file.objectURL.
changingThisBreaksApplicationSecurity.arrayBuffer();
reader.onload = (e) => e.target.result;
const expecteadBinaryData2 =reader.readAsDataURL(new Blob
([file.objectURL.changingThisBreaksApplicationSecurity]));
So I expect the result of expecteadBinaryData or expecteadBinaryData2 be a binary, but I get undefined. How should I get this?

Related

How to generate document with OpenAPI 3.0 that includes POST request with multipart/form-data to post both a JSON and a file in a single request? [duplicate]

I have the following swagger code addCustomer for sending a request that includes an image along with other json data. When I run code from swagger hub; it just sends json. Am I defining this right? Shouldn't it send multipart request?
post:
tags:
- customers
summary: Add a new customer to the store
description: ''
operationId: addCustomer
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/NewCustomerWithImageUrl'
application/xml:
schema:
$ref: '#/components/schemas/NewCustomerWithImageUrl'
multipart/form-data:
schema:
$ref: '#/components/schemas/NewCustomerWithImage'
description: Customer object that needs to be added to the store
required: true
NewCustomerWithImage:
type: object
allOf:
- $ref: '#/components/schemas/NewCustomer'
properties:
Image:
type: string
format: binary
Person:
type: object
required:
- first_name
properties:
first_name:
type: string
example: John
last_name:
type: string
example: Doe
email:
type: string
example: john#example.com
phone_number:
type: string
example: 555-555-5555
address_1:
type: string
example: 123 Nowhere Street
address_2:
type: string
example: Apartment 123
city:
type: string
example: Rochester
state:
type: string
example: New York
zip:
type: string
example: '14445'
country:
type: string
example: United States
comments:
type: string
example: A great customer
custom_fields:
type: object
minProperties: 0
maxProperties: 10
additionalProperties:
type: string
example:
secondary phone number: '555-555-5555'
NewCustomer:
type: object
allOf:
- $ref: '#/components/schemas/Person'
properties:
company_name:
type: string
example: PHP Point Of Sale
tier_id:
type: integer
format: uuid
example: 0
account_number:
type: string
example: '3333'
taxable:
type: boolean
example: false
tax_certificate:
type: string
example: '1234'
override_default_tax:
type: boolean
example: false
tax_class_id:
type: integer
format: uuid
example: 0
balance:
type: number
format: float
example: 22.99
credit_limit:
example: null
points:
type: integer
format: int32
example: 333
disable_loyalty:
type: boolean
example: true
amount_to_spend_for_next_point:
type: number
format: float
readOnly: true
example: 10.00
remaining_sales_before_discount:
type: integer
format: int32
readOnly: true
example: 0
xml:
name: Customer
Json sent:
{"first_name":"John","last_name":"Doe","email":"john#example.com","phone_number":"555-555-5555","address_1":"123 Nowhere Street","address_2":"Apartment 123","city":"Rochester","state":"New York","zip":"14445","country":"United States","comments":"A great customer","custom_fields":{"secondary phone number":"555-555-5555"},"company_name":"PHP Point Of Sale","tier_id":0,"account_number":"3333","taxable":false,"tax_certificate":"1234","override_default_tax":false,"tax_class_id":0,"balance":22.99,"credit_limit":null,"points":333,"disable_loyalty":true,"Image":"string"}
Swagger Hub File:
https://app.swaggerhub.com/apis/PHP-Point-Of-Sale/PHP-Point-Of-Sale/1.0#/customers/addCustomer
1) Swagger UI (which SwaggerHub uses) does not yet support form data and file upload for OpenAPI 3.0 definitions. Follow this issue for status updates: https://github.com/swagger-api/swagger-ui/issues/3641 UPD: Swagger UI now supports file uploads for OAS3.
2) If "request that includes an image along with other JSON data" means a multipart request like this –
POST /foo HTTP/1.1
Content-Type: multipart/form-data; boundary=ABCDEF123
--ABCDEF123
Content-Disposition: form-data; name="Customer"
Content-Type: application/json
{
"foo": "bar"
}
--ABCDEF123
Content-Disposition: form-data; name="Image"; filename="image1.png"
Content-Type: application/octet-steam
{…image content…}
--ABCDEF123--
then the request body definition should look like this:
requestBody:
content:
...
multipart/form-data:
schema:
type: object
properties:
Customer: # <--- name of the part in a multipart request
$ref: '#/components/schemas/NewCustomer'
Image: # <--- name of the part in a multipart request
type: string
format: binary
required:
- Customer
- Image
More information:
How to describe a multipart response using OpenAPI (Swagger)? here on SO
Special Considerations for multipart Content in the OpenAPI 3.0 Specification
Multipart Requests on swagger.io
File upload is supported in Open API v 3.0.3
Here's what my swagger.json looks like:
"/media/upload": {
"post": {
"tags": ["Media"],
"name": "Upload Media",
"description": "Uploads a Media file to the server.",
"requestBody": {
"required": true,
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"media": {
"type": "string",
"format": "base64"
}
}
}
}
}
}
}
}
Here's how it show up in swagger:

Pass-through heterogeneous (non-uniform) JSON column in Spark

We are processing data with Apache Spark 3.1.x where one field contains fully free-form JSON, so the individual records can contain same keys, but with different datatypes (payload.field1 in this example can be string, boolean or number):
{"timestamp": "2021-07-30T09:41:51Z", "payload": {"field1": "some text"}}
{"timestamp": "2021-07-30T09:41:52Z", "payload": {"field1": true}}
{"timestamp": "2021-07-30T09:41:53Z", "payload": {"field1": 123}}
Our goal is to keep payload field intact. When we let Spark autodetect the schema:
Dataset<Row> events = spark.read().json("file:////users/user/input.json");
// some processing is going on here
events.write().json("file:///users/user/output.json");
Output is as following (notice payload.field now being a string):
{"payload":{"field1":"some text"},"timestamp":"2021-07-30T09:41:51Z"}
{"payload":{"field1":"true"},"timestamp":"2021-07-30T09:41:51Z"}
{"payload":{"field1":"123"},"timestamp":"2021-07-30T09:41:52Z"}
Spark's printSchema() output:
root
|-- payload: struct (nullable = true)
| |-- field1: string (nullable = true)
|-- timestamp: string (nullable = true)
The best workaround we came up so far is this:
Dataset<String> eventsAsString = spark.read().text("file:////users/user/input.json").as(Encoders.STRING());
Dataset<Row> events2 = eventsAsString.select( //
get_json_object(col("value"), "$.timestamp").alias("timestamp"), //
get_json_object(col("value"), "$.payload").alias("payload") // This will keep payload as string for Spark
);
// Do some processing of events here
// We have to write JSON as string to prevent Spark from encoding payload's field JSON:
events2.withColumn("joined", concat( //
format_string("{\"timestamp\":\"%s\", ", col("timestamp")), //
format_string("\"payload\":%s}", col("payload")) //
)).select(col("joined")).write().text("file:///users/user/output.txt");
Output we get is what we want, datatypes are unchanged:
{"timestamp":"2021-07-30T09:41:51Z", "payload":{"field1":"some text"}}
{"timestamp":"2021-07-30T09:41:52Z", "payload":{"field1":true}}
{"timestamp":"2021-07-30T09:41:53Z", "payload":{"field1":123}}
The solution above works but feels super hacky. Maybe we are missing something obvious here?
Thanks in advance!
For read, we can specific the schema before read.
For write, I don't come up with better idea.
val schema = StructType(Array[StructField](
StructField("timestamp", DataTypes.StringType, false, Metadata.empty),
StructField("payload", DataTypes.StringType, false, Metadata.empty)) // force string type
)
val df: Dataset[Row] = spark.read.schema(schema).json(ds)
df.map(r => "{\"timestamp\": \"%s\", \"payload\": %s".format(r.getString(0), r.getString(1)))
.write.text("xxx")

Pretty print String to JSON format

I execute an AWS command to retrieve the spot price history.
DescribeSpotPriceHistoryRequest request = new DescribeSpotPriceHistoryRequest().withEndTime(start)
.withInstanceTypes("m1.xlarge").withProductDescriptions("Linux/UNIX (Amazon VPC)").withStartTime(end);
DescribeSpotPriceHistoryResult response = client.describeSpotPriceHistory(request);
System.out.println(response.toString());
I obtained the result in json format but I receive it in String like:
{SpotPriceHistory: [{AvailabilityZone: us-east-1d,InstanceType: m1.xlarge,ProductDescription: Linux/UNIX,SpotPrice: 0.035000,Timestamp: Wed Nov 07 00:18:50 CET 2018}, {AvailabilityZone: us-east-1c,InstanceType: m1.xlarge,ProductDescription: Linux/UNIX,SpotPrice: 0.035000,Timestamp: Wed Nov 07 00:18:50 CET 2018}, {AvailabilityZone: us-east-1b,InstanceType: m1.xlarge,ProductDescription: Linux/UNIX,SpotPrice: 0.035000,Timestamp: Wed Nov 07 00:18:50 CET 2018}, {AvailabilityZone: us-east-1a,InstanceType: m1.xlarge,ProductDescription: Linux/UNIX,SpotPrice: 0.035000,Timestamp: Wed Nov 07 00:18:50 CET 2018}, {AvailabilityZone: us-east-1e,InstanceType: m1.xlarge,ProductDescription: Linux/UNIX,SpotPrice: 0.350000,Timestamp: Thu Sep 20 01:08:39 CEST 2018}]}
my question is: How can I improve the displaying of the results ? like
{
"Timestamp": "2018-09-08T17:07:14.000Z",
"AvailabilityZone": "us-east-1d",
"InstanceType": "p2.16xlarge",
"ProductDescription": "Linux/UNIX",
"SpotPrice": "4.320000"
},
{
"Timestamp": "2018-09-08T17:07:14.000Z",
"AvailabilityZone": "us-east-1c",
"InstanceType": "p2.16xlarge",
"ProductDescription": "Linux/UNIX",
"SpotPrice": "4.320000"
},
{
"Timestamp": "2018-09-08T17:07:14.000Z",
"AvailabilityZone": "us-east-1b",
"InstanceType": "p2.16xlarge",
"ProductDescription": "Linux/UNIX",
"SpotPrice": "4.320000"
},
{
"Timestamp": "2018-09-08T12:32:28.000Z",
"AvailabilityZone": "us-east-1e",
"InstanceType": "p2.16xlarge",
"ProductDescription": "Linux/UNIX",
"SpotPrice": "4.320000"
}
]
}
You're calling the .toString() on the response object, just be careful with this as there is no guarantee that will always be json, as you're seeing above, it's not even valid json as it's missing quotes around the attribute names and values.
One option to get what you want is to call response.getSpotPriceHistory() to get you the array of spot prices, then pass that thru ObjectMapper and write it as a pretty string, like so:
public static void main(String[] args) throws IOException {
AmazonEC2 client = AmazonEC2Client.builder().build();
DescribeSpotPriceHistoryRequest request = new DescribeSpotPriceHistoryRequest()
.withEndTime(new Date())
.withInstanceTypes("m1.xlarge").withProductDescriptions("Linux/UNIX (Amazon VPC)")
.withStartTime(new Date());
DescribeSpotPriceHistoryResult response = client.describeSpotPriceHistory(request);
ObjectMapper mapper = new ObjectMapper();
String asPrettyJSon = mapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(response.getSpotPriceHistory());
System.out.println(asPrettyJSon);
}
Both represent a json array containing jsonObjects of same structure.
Displaying result will depend on your front implementation not the layaout of your jsonRespense.

Error JSON object SyntaxError: JSON Parse error: Unterminated string withSpring MVC

Help me Please.
JSON object SyntaxError: JSON Parse error: Unterminated string
JAVA code
#RequestMapping(value = "getMessage.htm")
public #ResponseBody String getStatusServer(ModelMap model, HttpSession session,#RequestParam("key") String key)
throws InterruptedException, ExecutionException {
BufferData bufferData = DataMap.dataMap.get(key);
StringBuilder content = new StringBuilder();
content.append("{\"status\":").append(bufferData.getStatus()).append(",").append(" \"messages\": \"").append(bufferData.getMess()).append("\"}");
System.out.println(content.toString());
return content.toString();
}
JQuery code
function getMessage() {
$.ajax({
type : 'GET',
url : "<c:url value="/"/>" + "getMessage.htm",
data : 'key=' + 'job1',
dataType : "json",
success : function(data) {
alert("test");
alert(JSON.stringify(data));
},
error : function(data, status, er) {
alert("error: " + data + " status: " + status + " er:" + er);
}
});
}
Error code
SyntaxError: JSON Parse error: Unterminated string
Help me Please.
and json output
{"status":1, "messages":"Welcome to Ubuntu 12.04.2 LTS (GNU/Linux 3.5.0-23-generic x86_64) * Documentation: https://help.ubuntu.com/ System information as of Fri Jun 21 17:13:41 ICT 2013 System load: 0.08 Processes: 265 Usage of /: 13.9% of 38.02GB Users logged in: 2 Memory usage: 51% IP address for eth0: 10.216.92.20 Swap usage: 31% => There are 2 zombie processes. Graph this data and manage this system at https://landscape.canonical.com/ Last login: Fri Jun 21 16:30:33 2013 from wachirawat.local vos1#icenaja:~$ hostname icenaja vos1#icenaja:~$ pwd /home/vos1 vos1#icenaja:~$ exit logout "}
This does not look like valid json, try the below to see if it then will send.
data = {
"key": "job1"
}
I am not sure, what do you want to do with line.
url : "<c:url value="/"/>" + "getMessage.htm",
If this is nexted string value, try this single codes instead of double.
url : "<c:url value='/'/>" + "getMessage.htm"

jQuery access java Map of string object where the object is a map of strings

I am trying to return two json sets from java which each contain key/value pairs. I can get the data to return as expected but once I have the data I can not access it properly. Here is what my data coming from the java looks like
{"RESULTS":
{"MAP_1":
[
{"value":"1","display":"output text","type":"type a"},
{"value":"2","display":"more output text","type":"type a"}
],
"MAP_2":
[
{"value":"1","display":"output text","type":"type b"},
{"value":"2","display":"more output text","type":"type b"}
]
}
}
I have tried using $.map and $.each but I can not seem to drill into the data any help would be greatly appeciated.
Here is my latest attempt:
$.ajax({
url: url,
dataType: "text",
data: {
searchString: request.term
},
success: function( data ) {
response( $.map( data.MAP_1, function( item ) {
label: item.value + ", " + item.type
value: item.display
}));
}
});
Thanks in advance!
The format of the data returned by java is text, not json. So you should specify the dataType as json. In addition the following code are not right, I think.
data.MAP_1
should be
data.RESULTS.MAP_1

Categories