Skip to content
Snippets Groups Projects
Commit 8d630a59 authored by gmantele's avatar gmantele
Browse files

[UWS,TAP] Set UTF-8 as default character encoding for all HTTP response.

parent 998d11f5
No related branches found
No related tags found
No related merge requests found
Showing
with 84 additions and 20 deletions
......@@ -37,6 +37,7 @@ import tap.parameters.DALIUpload;
import tap.parameters.TAPParameters;
import tap.upload.Uploader;
import uws.UWSException;
import uws.UWSToolBox;
import uws.job.JobThread;
import uws.job.Result;
import uws.service.log.UWSLog.LogLevel;
......@@ -610,6 +611,9 @@ public class ADQLExecutor {
// Set the HTTP content type to the MIME type of the result format:
response.setContentType(formatter.getMimeType());
// Set the character encoding:
response.setCharacterEncoding(UWSToolBox.DEFAULT_CHAR_ENCODING);
// Write the formatted result in the HTTP response output:
start = System.currentTimeMillis();
writeResult(queryResult, formatter, response.getOutputStream());
......
......@@ -41,6 +41,7 @@ import tap.formatter.VOTableFormat;
import tap.log.DefaultTAPLog;
import tap.log.TAPLog;
import uws.UWSException;
import uws.UWSToolBox;
import uws.job.ErrorSummary;
import uws.job.ErrorType;
import uws.job.UWSJob;
......@@ -193,6 +194,9 @@ public class DefaultTAPErrorWriter implements ServiceErrorWriter {
// Set the MIME type of the answer (XML for a VOTable document):
response.setContentType("application/xml");
// Set the character encoding:
response.setCharacterEncoding(UWSToolBox.DEFAULT_CHAR_ENCODING);
}catch(IllegalStateException ise){
/* If it is not possible any more to reset the response header and body,
* the error is anyway written in order to corrupt the HTTP response.
......
......@@ -456,6 +456,7 @@ public class TAPMetadata implements Iterable<TAPSchema>, VOSIResource, TAPResour
@Override
public boolean executeResource(HttpServletRequest request, HttpServletResponse response) throws IOException{
response.setContentType("application/xml");
response.setCharacterEncoding(UWSToolBox.DEFAULT_CHAR_ENCODING);
PrintWriter writer = response.getWriter();
write(writer);
......
......@@ -16,7 +16,7 @@ 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 2012,2014 - UDS/Centre de Données astronomiques de Strasbourg (CDS),
* Copyright 2012-2015 - UDS/Centre de Données astronomiques de Strasbourg (CDS),
* Astronomisches Rechen Institut (ARI)
*/
......@@ -31,12 +31,13 @@ import javax.servlet.http.HttpServletResponse;
import tap.ServiceConnection;
import tap.TAPException;
import uk.ac.starlink.votable.VOSerializer;
import uws.UWSToolBox;
/**
* <p>TAP resource describing the availability of a TAP service.</p>
*
* @author Gr&eacute;gory Mantelet (CDS;ARI)
* @version 2.0 (09/2014)
* @version 2.0 (04/2015)
*/
public class Availability implements TAPResource, VOSIResource {
......@@ -109,6 +110,9 @@ public class Availability implements TAPResource, VOSIResource {
// Set the response MIME type (XML):
response.setContentType("text/xml");
// Set the character encoding:
response.setCharacterEncoding(UWSToolBox.DEFAULT_CHAR_ENCODING);
// Get the output stream:
PrintWriter pw = response.getWriter();
......
......@@ -31,6 +31,7 @@ import javax.servlet.http.HttpServletResponse;
import tap.TAPException;
import uk.ac.starlink.votable.VOSerializer;
import uws.UWSToolBox;
/**
* <p>TAP resource describing the capabilities of a TAP service.</p>
......@@ -38,7 +39,7 @@ import uk.ac.starlink.votable.VOSerializer;
* <p>This resource just return an XML document giving a description of the TAP service and list all its VOSI resources.</p>
*
* @author Gr&eacute;gory Mantelet (CDS;ARI)
* @version 2.0 (02/2015)
* @version 2.0 (04/2015)
*/
public class Capabilities implements TAPResource, VOSIResource {
......@@ -112,6 +113,9 @@ public class Capabilities implements TAPResource, VOSIResource {
// Set the response MIME type (XML):
response.setContentType("application/xml");
// Set the character encoding:
response.setCharacterEncoding(UWSToolBox.DEFAULT_CHAR_ENCODING);
// Get the response stream:
PrintWriter out = response.getWriter();
......
......@@ -120,8 +120,8 @@ public class HomePage implements TAPResource {
// Set the content type:
response.setContentType(tap.homePageMimeType);
// set character encoding:
response.setCharacterEncoding("UTF-8");
// Set the character encoding:
response.setCharacterEncoding(UWSToolBox.DEFAULT_CHAR_ENCODING);
// Get the character writer:
PrintWriter writer = response.getWriter();
......@@ -216,6 +216,9 @@ public class HomePage implements TAPResource {
// Set the content type: HTML document
response.setContentType("text/html");
// Set the character encoding:
response.setCharacterEncoding(UWSToolBox.DEFAULT_CHAR_ENCODING);
// Write the home page:
writer.println("<html><head><title>TAP HOME PAGE</title></head><body><h1 style=\"text-align: center\">TAP HOME PAGE</h1><h2>Available resources:</h2><ul>");
for(TAPResource res : tap.resources.values())
......
......@@ -825,6 +825,9 @@ public class TAP implements VOSIResource {
throw new TAPException(ue);
}
// Set the character encoding:
response.setCharacterEncoding(UWSToolBox.DEFAULT_CHAR_ENCODING);
// Display the TAP Home Page:
if (resourceName.length() == 0){
resourceName = homePage.getName();
......
......@@ -54,10 +54,15 @@ import uws.service.request.UploadFile;
* Some useful functions for the managing of a UWS service.
*
* @author Gr&eacute;gory Mantelet (CDS;ARI)
* @version 4.1 (03/2015)
* @version 4.1 (04/2015)
*/
public class UWSToolBox {
/**
* Default character encoding for all HTTP response sent by this library.
* @since 4.1 */
public final static String DEFAULT_CHAR_ENCODING = "UTF-8";
private static UWSLog defaultLogger = null;
/** <b>THIS CLASS CAN'T BE INSTANTIATED !</b> */
......@@ -504,6 +509,9 @@ public class UWSToolBox {
if (mimeType != null)
response.setContentType(mimeType);
// Set the character encoding:
response.setCharacterEncoding(UWSToolBox.DEFAULT_CHAR_ENCODING);
// Set the HTTP content length:
if (contentSize > 0)
response.setContentLength((int)contentSize);
......
......@@ -1128,6 +1128,9 @@ public class UWSService implements UWS {
// Identify the user:
user = UWSToolBox.getUser(request, userIdentifier);
// Set the character encoding:
response.setCharacterEncoding(UWSToolBox.DEFAULT_CHAR_ENCODING);
// Apply the appropriate UWS action:
for(int i = 0; action == null && i < uwsActions.size(); i++){
if (uwsActions.get(i).match(urlInterpreter, user, request)){
......@@ -1217,6 +1220,7 @@ public class UWSService implements UWS {
public void redirect(String url, HttpServletRequest request, JobOwner user, String uwsAction, HttpServletResponse response) throws IOException, UWSException{
response.setStatus(HttpServletResponse.SC_SEE_OTHER);
response.setContentType(request.getContentType());
response.setCharacterEncoding(UWSToolBox.DEFAULT_CHAR_ENCODING);
response.setHeader("Location", url);
response.flushBuffer();
}
......
......@@ -342,6 +342,9 @@ public abstract class UWSServlet extends HttpServlet implements UWS, UWSFactory
// Identify the user:
user = UWSToolBox.getUser(req, userIdentifier);
// Set the character encoding:
resp.setCharacterEncoding(UWSToolBox.DEFAULT_CHAR_ENCODING);
// METHOD GET:
if (method.equals("GET")){
// HOME PAGE:
......@@ -489,6 +492,7 @@ public abstract class UWSServlet extends HttpServlet implements UWS, UWSFactory
protected void writeHomePage(UWSUrl requestUrl, HttpServletRequest req, HttpServletResponse resp, JobOwner user) throws UWSException, ServletException, IOException{
UWSSerializer serializer = getSerializer(req.getHeader("Accept"));
resp.setContentType(serializer.getMimeType());
resp.setCharacterEncoding(UWSToolBox.DEFAULT_CHAR_ENCODING);
String serialization;
try{
serialization = serializer.getUWS(this);
......@@ -514,6 +518,7 @@ public abstract class UWSServlet extends HttpServlet implements UWS, UWSFactory
// Write the jobs list:
UWSSerializer serializer = getSerializer(req.getHeader("Accept"));
resp.setContentType(serializer.getMimeType());
resp.setCharacterEncoding(UWSToolBox.DEFAULT_CHAR_ENCODING);
try{
jobsList.serialize(resp.getOutputStream(), serializer, user);
}catch(Exception e){
......@@ -585,6 +590,7 @@ public abstract class UWSServlet extends HttpServlet implements UWS, UWSFactory
// Write the job summary:
UWSSerializer serializer = getSerializer(req.getHeader("Accept"));
resp.setContentType(serializer.getMimeType());
resp.setCharacterEncoding(UWSToolBox.DEFAULT_CHAR_ENCODING);
try{
job.serialize(resp.getOutputStream(), serializer, user);
}catch(Exception e){
......@@ -664,11 +670,15 @@ public abstract class UWSServlet extends HttpServlet implements UWS, UWSFactory
UWSSerializer serializer = getSerializer(req.getHeader("Accept"));
String uwsField = attributes[0];
boolean jobSerialization = false;
// Set the content type:
if (uwsField == null || uwsField.trim().isEmpty() || (attributes.length <= 1 && (uwsField.equalsIgnoreCase(UWSJob.PARAM_ERROR_SUMMARY) || uwsField.equalsIgnoreCase(UWSJob.PARAM_RESULTS) || uwsField.equalsIgnoreCase(UWSJob.PARAM_PARAMETERS)))){
resp.setContentType(serializer.getMimeType());
jobSerialization = true;
}else
resp.setContentType("text/plain");
// Set the character encoding:
resp.setCharacterEncoding(UWSToolBox.DEFAULT_CHAR_ENCODING);
// Serialize the selected attribute:
try{
job.serialize(resp.getOutputStream(), attributes, serializer);
}catch(Exception e){
......@@ -766,6 +776,7 @@ public abstract class UWSServlet extends HttpServlet implements UWS, UWSFactory
public void redirect(String url, HttpServletRequest request, JobOwner user, String uwsAction, HttpServletResponse response) throws ServletException, IOException{
response.setStatus(HttpServletResponse.SC_SEE_OTHER);
response.setContentType(request.getContentType());
response.setCharacterEncoding(UWSToolBox.DEFAULT_CHAR_ENCODING);
response.setHeader("Location", url);
response.flushBuffer();
}
......
......@@ -16,7 +16,7 @@ package uws.service.actions;
* 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 2012,2014 - UDS/Centre de Données astronomiques de Strasbourg (CDS),
* Copyright 2012-2015 - UDS/Centre de Données astronomiques de Strasbourg (CDS),
* Astronomisches Rechen Institut (ARI)
*/
......@@ -43,7 +43,7 @@ import uws.service.log.UWSLog.LogLevel;
* The response of this action is a redirection to the new job resource (that is to say: a redirection to the job summary of the new job).</p>
*
* @author Gr&eacute;gory Mantelet (CDS;ARI)
* @version 4.1 (08/2014)
* @version 4.1 (04/2015)
*/
public class AddJob extends UWSAction {
private static final long serialVersionUID = 1L;
......
......@@ -16,7 +16,7 @@ package uws.service.actions;
* 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 2012,2014 - UDS/Centre de Données astronomiques de Strasbourg (CDS),
* Copyright 2012-2015 - UDS/Centre de Données astronomiques de Strasbourg (CDS),
* Astronomisches Rechen Institut (ARI)
*/
......@@ -43,7 +43,7 @@ import uws.service.log.UWSLog.LogLevel;
* The response of this action is a redirection to the jobs list.</p>
*
* @author Gr&eacute;gory Mantelet (CDS;ARI)
* @version 4.1 (11/2014)
* @version 4.1 (04/2015)
*/
public class DestroyJob extends UWSAction {
private static final long serialVersionUID = 1L;
......
......@@ -16,7 +16,7 @@ package uws.service.actions;
* 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 2012,2014 - UDS/Centre de Données astronomiques de Strasbourg (CDS),
* Copyright 2012-2015 - UDS/Centre de Données astronomiques de Strasbourg (CDS),
* Astronomisches Rechen Institut (ARI)
*/
......@@ -50,7 +50,7 @@ import uws.service.request.UploadFile;
* The serializer is choosen in function of the HTTP Accept header.</p>
*
* @author Gr&eacute;gory Mantelet (CDS;ARI)
* @version 4.1 (09/2014)
* @version 4.1 (04/2015)
*/
public class GetJobParam extends UWSAction {
private static final long serialVersionUID = 1L;
......@@ -171,11 +171,16 @@ public class GetJobParam extends UWSAction {
UWSSerializer serializer = uws.getSerializer(request.getHeader("Accept"));
String uwsField = attributes[0];
boolean jobSerialization = false;
// Set the content type:
if (uwsField == null || uwsField.trim().isEmpty() || (attributes.length <= 1 && (uwsField.equalsIgnoreCase(UWSJob.PARAM_ERROR_SUMMARY) || uwsField.equalsIgnoreCase(UWSJob.PARAM_RESULTS) || uwsField.equalsIgnoreCase(UWSJob.PARAM_PARAMETERS)))){
response.setContentType(serializer.getMimeType());
jobSerialization = true;
}else
response.setContentType("text/plain");
// Set the character encoding:
response.setCharacterEncoding(UWSToolBox.DEFAULT_CHAR_ENCODING);
// Serialize the selected attribute:
try{
job.serialize(response.getOutputStream(), attributes, serializer);
}catch(Exception e){
......
......@@ -16,7 +16,7 @@ package uws.service.actions;
* 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 2012,2014 - UDS/Centre de Données astronomiques de Strasbourg (CDS),
* Copyright 2012-2015 - UDS/Centre de Données astronomiques de Strasbourg (CDS),
* Astronomisches Rechen Institut (ARI)
*/
......@@ -27,6 +27,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import uws.UWSException;
import uws.UWSToolBox;
import uws.job.UWSJob;
import uws.job.serializer.UWSSerializer;
import uws.job.user.JobOwner;
......@@ -43,7 +44,7 @@ import uws.service.log.UWSLog.LogLevel;
* This summary is serialized by the {@link UWSSerializer} choosed in function of the HTTP Accept header.</p>
*
* @author Gr&eacute;gory Mantelet (CDS;ARI)
* @version 4.1 (08/2014)
* @version 4.1 (04/2015)
*/
public class JobSummary extends UWSAction {
private static final long serialVersionUID = 1L;
......@@ -100,6 +101,7 @@ public class JobSummary extends UWSAction {
// Write the job summary:
UWSSerializer serializer = uws.getSerializer(request.getHeader("Accept"));
response.setContentType(serializer.getMimeType());
response.setCharacterEncoding(UWSToolBox.DEFAULT_CHAR_ENCODING);
try{
job.serialize(response.getOutputStream(), serializer, user);
}catch(Exception e){
......
......@@ -16,7 +16,7 @@ package uws.service.actions;
* 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 2012,2014 - UDS/Centre de Données astronomiques de Strasbourg (CDS),
* Copyright 2012-2015 - UDS/Centre de Données astronomiques de Strasbourg (CDS),
* Astronomisches Rechen Institut (ARI)
*/
......@@ -27,6 +27,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import uws.UWSException;
import uws.UWSToolBox;
import uws.job.JobList;
import uws.job.serializer.UWSSerializer;
import uws.job.user.JobOwner;
......@@ -43,7 +44,7 @@ import uws.service.log.UWSLog.LogLevel;
* This list is serialized by the {@link UWSSerializer} choosed in function of the HTTP Accept header.</p>
*
* @author Gr&eacute;gory Mantelet (CDS;ARI)
* @version 4.1 (08/2014)
* @version 4.1 (04/2015)
*/
public class ListJobs extends UWSAction {
private static final long serialVersionUID = 1L;
......@@ -99,6 +100,7 @@ public class ListJobs extends UWSAction {
// Write the jobs list:
UWSSerializer serializer = uws.getSerializer(request.getHeader("Accept"));
response.setContentType(serializer.getMimeType());
response.setCharacterEncoding(UWSToolBox.DEFAULT_CHAR_ENCODING);
try{
jobsList.serialize(response.getOutputStream(), serializer, user);
}catch(Exception e){
......
......@@ -16,7 +16,7 @@ package uws.service.actions;
* 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 2012,2014 - UDS/Centre de Données astronomiques de Strasbourg (CDS),
* Copyright 2012-2015 - UDS/Centre de Données astronomiques de Strasbourg (CDS),
* Astronomisches Rechen Institut (ARI)
*/
......@@ -43,7 +43,7 @@ import uws.service.log.UWSLog.LogLevel;
* The response of this action is a redirection to the job summary.</p>
*
* @author Gr&eacute;gory Mantelet (CDS;ARI)
* @version 4.1 (09/2014)
* @version 4.1 (04/2015)
*/
public class SetJobParam extends UWSAction {
private static final long serialVersionUID = 1L;
......
......@@ -16,7 +16,7 @@ package uws.service.actions;
* 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 2014 - Astronomisches Rechen Institut (ARI)
* Copyright 2014-2015 - Astronomisches Rechen Institut (ARI)
*/
import java.io.IOException;
......@@ -40,7 +40,7 @@ import uws.service.UWSUrl;
* <p><i><u>Note:</u> The corresponding name is {@link UWSAction#SET_UWS_PARAMETER}.</i></p>
*
* @author Gr&eacute;gory Mantelet (ARI)
* @version 4.1 (11/2014)
* @version 4.1 (04/2015)
* @since 4.1
*/
public class SetUWSParameter extends UWSAction {
......
......@@ -30,6 +30,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import uws.UWSException;
import uws.UWSToolBox;
import uws.job.serializer.UWSSerializer;
import uws.job.user.JobOwner;
import uws.service.UWSService;
......@@ -100,6 +101,7 @@ public class ShowHomePage extends UWSAction {
if (uws.isDefaultHomePage()){
UWSSerializer serializer = uws.getSerializer(request.getHeader("Accept"));
response.setContentType(serializer.getMimeType());
response.setCharacterEncoding(UWSToolBox.DEFAULT_CHAR_ENCODING);
// Get a short and simple serialization of this UWS:
String serialization;
try{
......@@ -127,6 +129,7 @@ public class ShowHomePage extends UWSAction {
BufferedReader reader = new BufferedReader(new InputStreamReader(homePageUrl.openStream()));
response.setContentType("text/html");
response.setCharacterEncoding(UWSToolBox.DEFAULT_CHAR_ENCODING);
PrintWriter writer = response.getWriter();
try{
String line = null;
......
......@@ -212,6 +212,9 @@ public class DefaultUWSErrorWriter implements ServiceErrorWriter {
// Set the MIME type of the answer (XML for a VOTable document):
response.setContentType(UWSSerializer.MIME_TYPE_HTML);
// Set the character encoding:
response.setCharacterEncoding(UWSToolBox.DEFAULT_CHAR_ENCODING);
}catch(IllegalStateException ise){
/* If it is not possible any more to reset the response header and body,
* the error is anyway written in order to corrupt the HTTP response.
......@@ -292,6 +295,9 @@ public class DefaultUWSErrorWriter implements ServiceErrorWriter {
// Set the MIME type of the answer (JSON):
response.setContentType(UWSSerializer.MIME_TYPE_JSON);
// Set the character encoding:
response.setCharacterEncoding(UWSToolBox.DEFAULT_CHAR_ENCODING);
}catch(IllegalStateException ise){
/* If it is not possible any more to reset the response header and body,
* the error is anyway written in order to corrupt the HTTP response.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment