Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Remove hard validation of Character Encoding.
Browse files Browse the repository at this point in the history
Character encoding is no longer returning an error when not UTF-8. Instead, it now returns a warning when not in UTF-8, ASCII.
  • Loading branch information
Carlos Munoz committed Dec 12, 2011
1 parent 68db78e commit 597ea94
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
Expand Up @@ -74,15 +74,15 @@ public class ResourceUtils
/**
* Newline character used for multi-line comments
*/
private static final char NEWLINE = '\n';
private static final char NEWLINE = '\n';

private static final String ZANATA_GENERATOR_PREFIX = "Zanata";
private static final String ZANATA_GENERATOR_PREFIX = "Zanata";

private static final String ZANATA_TAG = "#zanata";
private static final String ZANATA_TAG = "#zanata";

private static final String PO_DATE_FORMAT = "yyyy-MM-dd hh:mmZ";
private static final String PO_DATE_FORMAT = "yyyy-MM-dd hh:mmZ";

private static final String PO_VALID_CONTENT_TYPE = "charset=UTF-8";
private static final String[] PO_VALID_CONTENT_TYPES = {"charset=UTF-8", "charset=utf-8", "charset=ASCII"};


/**
Expand Down Expand Up @@ -1171,7 +1171,13 @@ public boolean validateResourceEncoding(Resource res)
{
if( entry.getKey().equalsIgnoreCase( CONTENT_TYPE_HDR ) )
{
return entry.getValue().contains( PO_VALID_CONTENT_TYPE );
for( String acceptedContentType : PO_VALID_CONTENT_TYPES )
{
if( entry.getValue().contains( acceptedContentType ) )
{
return true;
}
}
}
}

Expand Down
Expand Up @@ -415,7 +415,7 @@ public Response putResource(@PathParam("id") String idNoSlash, InputStream messa

Resource entity = RestUtils.unmarshall(Resource.class, messageBody, requestContentType, headers.getRequestHeaders());
log.debug("resource details: {0}", entity);
validateResourceEncoding(entity);
boolean validResourceEnc = this.resourceUtils.validateResourceEncoding(entity);

HDocument document = documentDAO.getByDocId(hProjectIteration, id);
HLocale hLocale = validateSourceLocale(entity.getLang());
Expand Down Expand Up @@ -476,6 +476,11 @@ else if (document.isObsolete())
{
copyClosestEquivalentTranslation(document.getId(), entity.getName(), projectSlug, iterationSlug);
}

if( !validResourceEnc )
{
response.entity("warning: potentially incompatible character encoding.");
}

log.debug("put resource successfully");
return response.tag(etag).build();
Expand Down Expand Up @@ -914,14 +919,6 @@ private void validateExtensions(String... validExt) throws WebApplicationExcepti
}
}

private void validateResourceEncoding(Resource res) throws WebApplicationException
{
if( !this.resourceUtils.validateResourceEncoding(res) )
{
throw new WebApplicationException(Response.status(Status.UNSUPPORTED_MEDIA_TYPE).entity("Unsupported Encoding: ").build());
}
}


public void copyClosestEquivalentTranslation(Long docId, String name, String projectSlug, String iterationSlug)
{
Expand Down
Expand Up @@ -3,6 +3,8 @@
import java.util.Iterator;
import java.util.List;

import org.zanata.rest.dto.extensions.gettext.HeaderEntry;
import org.zanata.rest.dto.extensions.gettext.PoHeader;
import org.zanata.rest.dto.extensions.gettext.PoTargetHeader;
import org.zanata.rest.dto.extensions.gettext.TranslationsResourceExtension;
import org.zanata.rest.dto.resource.AbstractResourceMeta;
Expand Down Expand Up @@ -56,5 +58,18 @@ static void clearPoTargetHeaders(TranslationsResource ... docs)
}
}
}

static void addPoHeader( Resource res, String key, String value )
{
PoHeader poHeader = res.getExtensions(true).findByType(PoHeader.class);

if( poHeader == null )
{
poHeader = new PoHeader();
res.getExtensions().add(poHeader);
}

poHeader.getEntries().add( new HeaderEntry(key, value) );
}

}
Expand Up @@ -715,8 +715,22 @@ public void getBadProject() throws Exception
ClientResponse<List<ResourceMeta>> response = badTransResource.get(null);
assertThat(response.getStatus(), is(404));
}

@Test
public void putUnexpectedEncodingDocument() throws Exception
{
Resource doc = this.createSourceDoc("DocumentWithUnexpectedEncoding", true);
ResourceTestUtil.addPoHeader(doc, "Content-Type", "application/x-publican; charset=UNACCEPTABLE");
ClientResponse<String> response = this.putResource(doc, "gettext");
assertThat(response.getEntity(), containsString("warning"));
}

// END of tests

private ClientResponse<String> putResource( Resource res, String extensions )
{
return transResource.putResource(res.getName(), res, new StringSet( extensions ));
}

private void expectDocs(boolean checkRevs, boolean checkLinksIgnored, Resource... docs)
{
Expand Down

0 comments on commit 597ea94

Please sign in to comment.