Skip to content

Commit 3d45f2c

Browse files
author
Yaniv Inbar
committed
1 parent da1aa99 commit 3d45f2c

File tree

2 files changed

+76
-0
lines changed
  • google-http-client/src

2 files changed

+76
-0
lines changed

google-http-client/src/main/java/com/google/api/client/xml/atom/Atom.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@
1414

1515
package com.google.api.client.xml.atom;
1616

17+
import com.google.api.client.http.HttpHeaders;
1718
import com.google.api.client.http.HttpMediaType;
1819
import com.google.api.client.util.Charsets;
1920
import com.google.api.client.util.Experimental;
21+
import com.google.api.client.util.Lists;
2022
import com.google.api.client.util.Preconditions;
23+
import com.google.api.client.util.escape.PercentEscaper;
2124
import com.google.api.client.xml.Xml;
2225

26+
import java.util.Arrays;
27+
2328
/**
2429
* {@link Experimental} <br/>
2530
* Atom Utilities.
@@ -45,6 +50,10 @@ public final class Atom {
4550
public static final String MEDIA_TYPE =
4651
new HttpMediaType("application/atom+xml").setCharsetParameter(Charsets.UTF_8).build();
4752

53+
/** Escaper for the {@code Slug} header. */
54+
private static final PercentEscaper SLUG_ESCAPER =
55+
new PercentEscaper(" !\"#$&'()*+,-./:;<=>?@[\\]^_`{|}~", false);
56+
4857
static final class StopAtAtomEntry extends Xml.CustomizeParser {
4958

5059
static final StopAtAtomEntry INSTANCE = new StopAtAtomEntry();
@@ -68,4 +77,18 @@ public static void checkContentType(String contentType) {
6877
Preconditions.checkArgument(HttpMediaType.equalsIgnoreParameters(MEDIA_TYPE, contentType),
6978
"Wrong content type: expected <" + MEDIA_TYPE + "> but got <%s>", contentType);
7079
}
80+
81+
/**
82+
* Sets the {@code "Slug"} header, properly escaping the header value. See <a
83+
* href="http://tools.ietf.org/html/rfc5023#section-9.7">The Slug Header</a>.
84+
*
85+
* @since 1.14
86+
*/
87+
public static void setSlugHeader(HttpHeaders headers, String value) {
88+
if (value == null) {
89+
headers.remove("Slug");
90+
} else {
91+
headers.set("Slug", Lists.newArrayList(Arrays.asList(SLUG_ESCAPER.escape(value))));
92+
}
93+
}
7194
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) 2013 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.api.client.xml;
16+
17+
import com.google.api.client.http.HttpHeaders;
18+
import com.google.api.client.xml.atom.Atom;
19+
20+
import junit.framework.TestCase;
21+
import org.junit.Assert;
22+
23+
import java.util.List;
24+
25+
/**
26+
* Tests {@link Atom}.
27+
*
28+
* @author Yaniv Inbar
29+
*/
30+
public class AtomTest extends TestCase {
31+
32+
@SuppressWarnings("unchecked")
33+
public void testSetSlugHeader() {
34+
HttpHeaders headers = new HttpHeaders();
35+
assertNull(headers.get("Slug"));
36+
subtestSetSlugHeader(headers, "value", "value");
37+
subtestSetSlugHeader(
38+
headers, " !\"#$&'()*+,-./:;<=>?@[\\]^_`{|}~", " !\"#$&'()*+,-./:;<=>?@[\\]^_`{|}~");
39+
subtestSetSlugHeader(headers, "%D7%99%D7%A0%D7%99%D7%91", "יניב");
40+
subtestSetSlugHeader(headers, null, null);
41+
}
42+
43+
@SuppressWarnings("unchecked")
44+
public void subtestSetSlugHeader(HttpHeaders headers, String expectedValue, String value) {
45+
Atom.setSlugHeader(headers, value);
46+
if (value == null) {
47+
assertNull(headers.get("Slug"));
48+
} else {
49+
Assert.assertArrayEquals(
50+
new String[] {expectedValue}, ((List<String>) headers.get("Slug")).toArray());
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)