10000 add Atom.setSlugHeader · robccan/google-http-java-client@70bc658 · GitHub
[go: up one dir, main page]

Skip to content

Commit 70bc658

Browse files
author
Yaniv Inbar
committed
1 parent fbff83f commit 70bc658

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,11 +14,16 @@
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;
20+
import com.google.api.client.util.Lists;
1921
import com.google.api.client.util.Preconditions;
22+
import com.google.api.client.util.escape.PercentEscaper;
2023
import com.google.api.client.xml.Xml;
2124

25+
import java.util.Arrays;
26+
2227
/**
2328
* @since 1.0
2429
* @author Yaniv Inbar
@@ -40,6 +45,10 @@ public final class Atom {
4045
public static final String MEDIA_TYPE =
4146
new HttpMediaType("application/atom+xml").setCharsetParameter(Charsets.UTF_8).build();
4247

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

4554
static final StopAtAtomEntry INSTANCE = new StopAtAtomEntry();
@@ -63,4 +72,18 @@ public static void checkContentType(String contentType) {
6372
Preconditions.checkArgument(HttpMediaType.equalsIgnoreParameters(MEDIA_TYPE, contentType),
6473
"Wrong content type: expected <" + MEDIA_TYPE + "> but got <%s>", contentType);
6574
}
75+
76+
/**
77+
* Sets the {@code "Slug"} header, properly escaping the header value. See <a
78+
* href="http://tools.ietf.org/html/rfc5023#section-9.7">The Slug Header</a>.
79+
*
80+
* @since 1.14
81+
*/
82+
public static void setSlugHeader(HttpHeaders headers, String value) {
83+
if (value == null) {
84+
headers.remove("Slug");
85+
} else {
86+
headers.set("Slug", Lists.newArrayList(Arrays.asList(SLUG_ESCAPER.escape(value))));
87+
}
88+
}
6689
}
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)
0