Skip to content

Commit 0b86924

Browse files
Merge branch 'master' into feature/log-improvement
2 parents 0962556 + 715fb6f commit 0b86924

File tree

26 files changed

+279
-83
lines changed

26 files changed

+279
-83
lines changed

common/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<dependency>
3939
<groupId>commons-io</groupId>
4040
<artifactId>commons-io</artifactId>
41-
<version>2.11.0</version>
41+
<version>2.12.0</version>
4242
</dependency>
4343
<dependency>
4444
<groupId>org.bouncycastle</groupId>

common/src/main/java/com/genexus/GXExternalCollection.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@ public Vector getStruct()
100100
}
101101

102102
@SuppressWarnings("unchecked")
103-
public ArrayList getExternalInstance() {
104-
ArrayList list = new ArrayList();
103+
public <E> ArrayList<E> getExternalInstance() {
104+
ArrayList<E> list = new ArrayList<>();
105105
for (T Item : this)
106106
{
107107
try
108108
{
109-
list.add(Item.getClass().getMethod("getExternalInstance", new Class[]{}).invoke(Item));
109+
list.add((E) Item.getClass().getMethod("getExternalInstance", new Class[]{}).invoke(Item));
110110
}
111111
catch (Exception e)
112112
{

common/src/main/java/com/genexus/internet/GXHttpClient.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,8 @@ protected void resetState()
483483

484484
public abstract String getString();
485485

486+
public abstract void unreadChunk();
487+
486488
public abstract String readChunk();
487489

488490
public abstract boolean getEof();

common/src/main/java/com/genexus/internet/HttpClient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,11 @@ public String getString()
253253
return session.getString();
254254
}
255255

256+
public void unreadChunk()
257+
{
258+
session.unreadChunk();
259+
}
260+
256261
public String readChunk()
257262
{
258263
return session.readChunk();

common/src/main/java/com/genexus/internet/HttpClientManual.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,9 @@ public boolean getEof() {
436436
return true;
437437
}
438438

439+
public void unreadChunk() {
440+
}
441+
439442
public String readChunk() {
440443
return getString();
441444
}

common/src/main/java/com/genexus/internet/IHttpClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public interface IHttpClient {
4848
public void getHeader(String name, double[] value);
4949
public InputStream getInputStream() throws IOException;
5050
public String getString();
51+
public void unreadChunk();
5152
public String readChunk();
5253
public boolean getEof();
5354
public void toFile(String fileName);

common/src/main/java/com/genexus/util/GXFileInfo.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public boolean createNewFile() throws IOException{
6868
return fileSource.createNewFile();
6969
}
7070
public boolean createNewFile(InputStream input) throws IOException{
71-
fromBytes(SpecificImplementation.GXutil.toByteArray(input));
71+
fromInputStream(input);
7272
return true;
7373
}
7474
public boolean delete(){
@@ -180,6 +180,16 @@ public void fromBytes(byte[] data) throws IOException
180180
destination.write(data, 0, data.length);
181181
}
182182
}
183+
private void fromInputStream(InputStream input) throws IOException
184+
{
185+
try (OutputStream output = new BufferedOutputStream(new FileOutputStream(fileSource))) {
186+
byte[] buffer = new byte[8192];
187+
int bytesRead;
188+
while ((bytesRead = input.read(buffer)) != -1) {
189+
output.write(buffer, 0, bytesRead);
190+
}
191+
}
192+
}
183193
public String readAllText(String encoding)throws IOException{
184194
return SpecificImplementation.FileUtils.readFileToString(fileSource, CommonUtil.normalizeEncodingName(encoding));
185195
}

gxcloudstorage-awss3-v1/src/main/java/com/genexus/db/driver/ExternalProviderS3V1.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import com.amazonaws.services.s3.AmazonS3;
1919
import com.amazonaws.services.s3.AmazonS3Client;
20-
import com.amazonaws.util.IOUtils;
2120

2221
import java.io.FileNotFoundException;
2322
import java.io.FileOutputStream;
@@ -220,19 +219,14 @@ else if (acl == ResourceAccessControlList.PublicReadWrite) {
220219
}
221220

222221
public String upload(String externalFileName, InputStream input, ResourceAccessControlList acl) {
223-
byte[] bytes;
224-
try {
225-
bytes = IOUtils.toByteArray(input);
222+
try (ExternalProviderHelper.InputStreamWithLength streamInfo = ExternalProviderHelper.getInputStreamContentLength(input)) {
226223
ObjectMetadata metadata = new ObjectMetadata();
227-
metadata.setContentLength(bytes.length);
228-
if (externalFileName.endsWith(".tmp")) {
229-
metadata.setContentType("image/jpeg");
230-
}
224+
metadata.setContentLength(streamInfo.contentLength);
225+
metadata.setContentType((externalFileName.endsWith(".tmp") && "application/octet-stream".equals(streamInfo.detectedContentType)) ? "image/jpeg" : streamInfo.detectedContentType);
226+
231227
String upload = "";
232-
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes)) {
233-
client.putObject(new PutObjectRequest(bucket, externalFileName, byteArrayInputStream, metadata).withCannedAcl(internalToAWSACL(acl)));
234-
upload = getResourceUrl(externalFileName, acl, defaultExpirationMinutes);
235-
}
228+
client.putObject(new PutObjectRequest(bucket, externalFileName, streamInfo.inputStream, metadata).withCannedAcl(internalToAWSACL(acl)));
229+
upload = getResourceUrl(externalFileName, acl, defaultExpirationMinutes);
236230
return upload;
237231
} catch (IOException ex) {
238232
logger.error("Error while uploading file to the external provider.", ex);

gxcloudstorage-awss3-v2/src/main/java/com/genexus/db/driver/ExternalProviderS3V2.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@
2121
import com.genexus.util.StorageUtils;
2222
import com.genexus.StructSdtMessages_Message;
2323
import software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest;
24-
import software.amazon.awssdk.utils.IoUtils;
2524

2625
import java.io.*;
2726
import java.net.URI;
2827
import java.net.URL;
2928
import java.net.URLEncoder;
30-
import java.nio.ByteBuffer;
3129
import java.nio.charset.StandardCharsets;
3230
import java.nio.file.Files;
3331
import java.nio.file.Path;
@@ -603,8 +601,7 @@ else if (acl == ResourceAccessControlList.PublicReadWrite)
603601
}
604602

605603
private String uploadWithACL(String externalFileName, InputStream input, ResourceAccessControlList acl) {
606-
try {
607-
ByteBuffer byteBuffer = ByteBuffer.wrap(IoUtils.toByteArray(input));
604+
try (ExternalProviderHelper.InputStreamWithLength streamInfo = ExternalProviderHelper.getInputStreamContentLength(input)) {
608605
PutObjectRequest.Builder putObjectRequestBuilder = PutObjectRequest.builder()
609606
.bucket(bucket)
610607
.key(externalFileName)
@@ -613,7 +610,7 @@ private String uploadWithACL(String externalFileName, InputStream input, Resourc
613610
putObjectRequestBuilder = putObjectRequestBuilder.acl(internalToAWSACLWithACL(acl));
614611
PutObjectRequest putObjectRequest = putObjectRequestBuilder.build();
615612

616-
PutObjectResponse response = client.putObject(putObjectRequest, RequestBody.fromByteBuffer(byteBuffer));
613+
PutObjectResponse response = client.putObject(putObjectRequest, RequestBody.fromInputStream(streamInfo.inputStream, streamInfo.contentLength));
617614
if (!response.sdkHttpResponse().isSuccessful()) {
618615
logger.error("Error while uploading file: " + response.sdkHttpResponse().statusText().orElse("Unknown error"));
619616
}
@@ -727,15 +724,14 @@ private String uploadWithoutACL(String localFile, String externalFileName) {
727724
}
728725

729726
private String uploadWithoutACL(String externalFileName, InputStream input) {
730-
try {
731-
ByteBuffer byteBuffer = ByteBuffer.wrap(IoUtils.toByteArray(input));
727+
try (ExternalProviderHelper.InputStreamWithLength streamInfo = ExternalProviderHelper.getInputStreamContentLength(input)) {
732728
PutObjectRequest.Builder putObjectRequestBuilder = PutObjectRequest.builder()
733729
.bucket(bucket)
734730
.key(externalFileName)
735-
.contentType(externalFileName.endsWith(".tmp") ? "image/jpeg" : null);
731+
.contentType((externalFileName.endsWith(".tmp") && "application/octet-stream".equals(streamInfo.detectedContentType)) ? "image/jpeg" : streamInfo.detectedContentType);
736732
PutObjectRequest putObjectRequest = putObjectRequestBuilder.build();
737733

738-
PutObjectResponse response = client.putObject(putObjectRequest, RequestBody.fromByteBuffer(byteBuffer));
734+
PutObjectResponse response = client.putObject(putObjectRequest, RequestBody.fromInputStream(streamInfo.inputStream, streamInfo.contentLength));
739735
if (!response.sdkHttpResponse().isSuccessful()) {
740736
logger.error("Error while uploading file: " + response.sdkHttpResponse().statusText().orElse("Unknown error"));
741737
}

gxcloudstorage-azureblob/src/main/java/com/genexus/db/driver/ExternalProviderAzureStorage.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,14 @@ public String upload(String localFile, String externalFileName, ResourceAccessCo
139139
}
140140

141141
public String upload(String externalFileName, InputStream input, ResourceAccessControlList acl) {
142-
143-
try {
142+
try (ExternalProviderHelper.InputStreamWithLength streamInfo = ExternalProviderHelper.getInputStreamContentLength(input)) {
144143
CloudBlockBlob blob = getCloudBlockBlob(externalFileName, acl);
145-
if (externalFileName.endsWith(".tmp")) {
146-
blob.getProperties().setContentType("image/jpeg");
147-
}
144+
blob.getProperties().setContentType((externalFileName.endsWith(".tmp") && "application/octet-stream".equals(streamInfo.detectedContentType)) ? "image/jpeg" : streamInfo.detectedContentType);
148145
try (BlobOutputStream blobOutputStream = blob.openOutputStream()) {
149-
int next = input.read();
150-
while (next != -1) {
151-
blobOutputStream.write(next);
152-
next = input.read();
146+
byte[] buffer = new byte[8192];
147+
int bytesRead;
148+
while ((bytesRead = streamInfo.inputStream.read(buffer)) != -1) {
149+
blobOutputStream.write(buffer, 0, bytesRead);
153150
}
154151
}
155152
return getResourceUrl(externalFileName, acl, DEFAULT_EXPIRATION_MINUTES);

0 commit comments

Comments
 (0)