Skip to content
Snippets Groups Projects
Commit fea5ccf9 authored by Grégory Mantelet's avatar Grégory Mantelet
Browse files

[UWS,TAP] Fix HTTPServletResponse.setContentLenght for LONG values.

_This commit completes the GitHub issue #106 ._
parent b981e429
No related branches found
No related tags found
No related merge requests found
......@@ -16,7 +16,8 @@ package tap.resource;
* You should have received a copy of the GNU Lesser General Public License
* along with TAPLibrary. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2015-2017 - Astronomisches Rechen Institut (ARI)
* Copyright 2015-2019 - Astronomisches Rechen Institut (ARI),
* UDS/Centre de Données astronomiques de Strasbourg (CDS)
*/
import java.io.BufferedReader;
......@@ -50,8 +51,8 @@ import uws.service.log.UWSLog.LogLevel;
*
* <p><i>See {@link #forward(String, String, HttpServletRequest, HttpServletResponse)} for more details</i></p>
*
* @author Gr&eacute;gory Mantelet (ARI)
* @version 2.1 (03/2017)
* @author Gr&eacute;gory Mantelet (ARI;CDS)
* @version 2.2 (03/2019)
* @since 2.1
*/
public abstract class ForwardResource implements TAPResource {
......@@ -154,7 +155,7 @@ public abstract class ForwardResource implements TAPResource {
File f = new File(uri.getPath());
if (f.exists() && !f.isDirectory() && f.canRead()){
// set the content length:
response.setContentLength((int)f.length());
UWSToolBox.setContentLength(response, f.length());
// get the input stream:
input = new BufferedReader(new FileReader(f));
......
package uws;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
* This file is part of UWSLibrary.
*
......@@ -31,25 +51,11 @@ import uws.service.log.UWSLog;
import uws.service.request.RequestParser;
import uws.service.request.UploadFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.lang.reflect.Array;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
/**
* Some useful functions for the managing of a UWS service.
*
* @author Gr&eacute;gory Mantelet (CDS;ARI)
* @version 4.3 (01/2019)
* @version 4.4 (03/2019)
*/
public class UWSToolBox {
......@@ -525,8 +531,7 @@ public class UWSToolBox {
response.setCharacterEncoding(UWSToolBox.DEFAULT_CHAR_ENCODING);
// Set the HTTP content length:
if (contentSize > 0)
response.setHeader("Content-Length", String.valueOf(contentSize));
setContentLength(response, contentSize);
// Write the file into the HTTP response:
output = response.getOutputStream();
......@@ -688,4 +693,31 @@ public class UWSToolBox {
return null;
}
/**
* Set the content length in the given {@link HttpServletResponse}.
*
* <p><i><b>Implementation note:</b>
* This could perfectly be done using
* {@link HttpServletResponse#setContentLength(int)}, <b>but only if the
* content size is encoded or fit in an integer value</b>. Otherwise, that
* function will set no content length.
* On the contrary, this current function takes a long value and set
* manually the content type header.
* </i></p>
*
* <p><i><b>Note:</b>
* This function has no effect if the given {@link HttpServletResponse} is
* NULL or if the given content size is &le; 0.
* </i></p>
*
* @param response HTTP response.
* @param contentSize The content size to set.
*
* @since 4.4
*/
public static final void setContentLength(final HttpServletResponse response, final long contentSize){
if (response != null && contentSize > 0)
response.setHeader("Content-Length", String.valueOf(contentSize));
}
}
......@@ -16,7 +16,8 @@ package uws.job.jobInfo;
* You should have received a copy of the GNU Lesser General Public License
* along with UWSLibrary. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2017 - Astronomisches Rechen Institut (ARI)
* Copyright 2017-2019 - Astronomisches Rechen Institut (ARI),
* UDS/Centre de Données astronomiques de Strasbourg (CDS)
*/
import java.io.IOException;
......@@ -25,6 +26,7 @@ import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import uws.UWSException;
import uws.UWSToolBox;
import uws.job.UWSJob;
import uws.job.serializer.XMLSerializer;
......@@ -47,8 +49,8 @@ import uws.job.serializer.XMLSerializer;
* with {@link #setValue(String)}.</li>
* </ul>
*
* @author Gr&eacute;gory Mantelet (ARI)
* @version 4.2 (06/2017)
* @author Gr&eacute;gory Mantelet (ARI;CDS)
* @version 4.4 (03/2019)
* @since 4.2
*/
public class SingleValueJobInfo implements JobInfo {
......@@ -174,7 +176,7 @@ public class SingleValueJobInfo implements JobInfo {
public void write(HttpServletResponse response) throws IOException, UWSException{
response.setCharacterEncoding("UTF-8");
response.setContentType("text/xml");
response.setContentLength(xmlRepresentation.getBytes("UTF-8").length);
UWSToolBox.setContentLength(response, xmlRepresentation.getBytes("UTF-8").length);
PrintWriter writer = response.getWriter();
writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
......
......@@ -16,7 +16,8 @@ package uws.job.jobInfo;
* You should have received a copy of the GNU Lesser General Public License
* along with UWSLibrary. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2017 - Astronomisches Rechen Institut (ARI)
* Copyright 2017-2019 - Astronomisches Rechen Institut (ARI),
* UDS/Centre de Données astronomiques de Strasbourg (CDS)
*/
import java.io.BufferedReader;
......@@ -140,8 +141,8 @@ import uws.service.request.UploadFile;
* function {@link #restoreFile()}.
* </p>
*
* @author Gr&eacute;gory Mantelet (ARI)
* @version 4.2 (06/2017)
* @author Gr&eacute;gory Mantelet (ARI;CDS)
* @version 4.4 (03/2019)
* @since 4.2
*/
public class XMLJobInfo implements JobInfo {
......@@ -277,7 +278,8 @@ public class XMLJobInfo implements JobInfo {
if (input != null){
try{
input.close();
}catch(IOException ioe){}
}catch(IOException ioe){
}
}
}
return xml.toString();
......@@ -290,7 +292,7 @@ public class XMLJobInfo implements JobInfo {
if (content != null){
response.setCharacterEncoding("UTF-8");
response.setContentType("text/xml");
response.setContentLength(content.getBytes("UTF-8").length);
UWSToolBox.setContentLength(response, content.getBytes("UTF-8").length);
PrintWriter writer = response.getWriter();
writer.println(content);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment