Skip to content

Commit 1d7f940

Browse files
committed
SD image requests - adding logging for testing 12
1 parent f5664ea commit 1d7f940

File tree

2 files changed

+45
-26
lines changed

2 files changed

+45
-26
lines changed

java/sage/MetaImage.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3273,25 +3273,25 @@ private boolean loadCacheFile()
32733273
if ("application/json".equals(httpConn.getContentType())){
32743274
//SD returned an error which is within the returned json
32753275
//TODO: remove after testing
3276-
if (Sage.DBG) System.out.println("MetaImage.loadCacheFile: Prior to ErrorHandling");
3277-
SDUtils.handleSDJsonErrorFromHttpResponse(httpConn);
3278-
if (Sage.DBG) System.out.println("MetaImage.loadCacheFile: After ErrorHandling");
3279-
is.close();
3280-
if (Sage.DBG) System.out.println("MetaImage.loadCacheFile: is closed");
3281-
is = null;
3282-
if (Sage.DBG) System.out.println("MetaImage.loadCacheFile: is nul now");
3283-
break;
3284-
}else{
3285-
if (Sage.DBG) System.out.println("MetaImage.loadCacheFile: ELSE: content type:" + httpConn.getContentType());
3286-
3287-
if (Sage.DBG) System.out.println("MetaImage.loadCacheFile: Prior to ErrorHandling");
3288-
SDUtils.handleSDJsonErrorFromHttpResponse(httpConn);
3289-
if (Sage.DBG) System.out.println("MetaImage.loadCacheFile: After ErrorHandling");
3290-
is.close();
3291-
if (Sage.DBG) System.out.println("MetaImage.loadCacheFile: is closed");
3292-
is = null;
3293-
if (Sage.DBG) System.out.println("MetaImage.loadCacheFile: is nul now");
3294-
break;
3276+
int imageErrorCode = SDUtils.handleSDJsonErrorFromHttpResponse(httpConn);
3277+
if(imageErrorCode==1004){ //no or invalid token
3278+
is.close();
3279+
is = null;
3280+
break;
3281+
}else if(imageErrorCode==5000){ //image does not exist - do not ask again
3282+
//create a blank image to store in cache so next request returns the blank rather than asking for an image that does not exist in SD
3283+
if (Sage.DBG) System.out.println("MetaImage.loadCacheFile: error 5000 - SD image does not exist - creating a blank image for the cache to avoid re-requesting");
3284+
is = SDUtils.createBlankImageInputStream(270,360,"jpg");
3285+
break;
3286+
}else if(imageErrorCode==5002 || imageErrorCode==5003){ //5002-max downloads, 5003-max downloads trial
3287+
is.close();
3288+
is = null;
3289+
break;
3290+
}else{
3291+
is.close();
3292+
is = null;
3293+
break;
3294+
}
32953295
}
32963296
}
32973297
if (httpConn.getResponseCode() / 100 == 3)

java/sage/epg/sd/SDUtils.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@
6161
import java.util.List;
6262
import java.util.TimeZone;
6363
import java.util.zip.GZIPInputStream;
64+
import java.awt.image.BufferedImage;
65+
import java.io.ByteArrayInputStream;
66+
import java.io.ByteArrayOutputStream;
67+
import java.io.IOException;
68+
import javax.imageio.ImageIO;
6469

6570
public class SDUtils
6671
{
@@ -83,7 +88,7 @@ public class SDUtils
8388
GSON = gsonBuilder.create();
8489
}
8590

86-
public static void handleSDJsonErrorFromHttpResponse(HttpURLConnection httpConn) throws IOException, SDException
91+
public static int handleSDJsonErrorFromHttpResponse(HttpURLConnection httpConn)
8792
{
8893
InputStream inputStream = new BufferedInputStream(httpConn.getInputStream());
8994
InputStreamReader reader = new InputStreamReader(inputStream, SDSession.IN_CHARSET);
@@ -92,19 +97,33 @@ public static void handleSDJsonErrorFromHttpResponse(HttpURLConnection httpConn)
9297

9398
if (errorElement instanceof JsonObject)
9499
{
95-
if (Sage.DBG) System.out.println("SDUtils.handleSDJsonErrorFromHttpResponse: JsonObject found");
96100
JsonElement codeElement = ((JsonObject) errorElement).get("code");
97-
if (Sage.DBG) System.out.println("SDUtils.handleSDJsonErrorFromHttpResponse: JsonElement:" + codeElement);
98101
int code = codeElement != null ? codeElement.getAsInt() : -1;
99-
100-
if (Sage.DBG) System.out.println("SDUtils.handleSDJsonErrorFromHttpResponse: Error code:" + code);
101-
102-
//SDErrors.throwErrorForCode(code);
103102
if (Sage.DBG) System.out.println("SDUtils.handleSDJsonErrorFromHttpResponse: Error:" + code + " : " + SDErrors.getErrorForCode(code));
103+
return code;
104104
}else{
105105
if (Sage.DBG) System.out.println("SDUtils.handleSDJsonErrorFromHttpResponse: Unknown Error");
106+
return -9999;
106107
}
107108
}
109+
110+
public static ByteArrayInputStream createBlankImageInputStream(int width, int height, String format) {
111+
// Create a blank BufferedImage (white background)
112+
BufferedImage blankImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
113+
// Fill the image with white color
114+
blankImage.createGraphics().fillRect(0, 0, width, height);
115+
116+
// Convert BufferedImage to byte array using ImageIO
117+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
118+
try {
119+
ImageIO.write(blankImage, format, baos);
120+
} catch (IOException e) {
121+
throw new RuntimeException("Failed to write blank image to stream", e);
122+
}
123+
124+
// Convert byte array to ByteArrayInputStream
125+
return new ByteArrayInputStream(baos.toByteArray());
126+
}
108127

109128
/**
110129
* Determine what kind of stream is returned and wrap it with an appropriate processing layer.

0 commit comments

Comments
 (0)