Use IdString to wrap encoded strings in REST API calls

Clients supply encoded strings as the id portion of REST API
paths. These strings need to be decoded within the view code
as some encodings are specially handled. Try to prevent misuse
by wrapping strings in a type-safe IdString wrapper that forces
developers to decode with ".get()" before using.

The raw string can be obtained with ".encoded()", an obvious
hint that the return value will still be URL encoded. This is
used in only a handful of locations where special parsing must
be performed.

Change-Id: Ied5747d1fe8cc0f2dc24017f7aacc2fcab69a37c
This commit is contained in:
Shawn Pearce
2013-01-28 22:09:07 -08:00
parent a1876adf9a
commit a0610125bd
20 changed files with 179 additions and 73 deletions

View File

@@ -18,9 +18,10 @@ import com.google.common.base.Objects;
import com.google.common.base.Strings;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.Response;
import com.google.gerrit.extensions.restapi.IdString;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.Response;
import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.IdentifiedUser;
@@ -73,7 +74,9 @@ class SetDefaultDashboard implements RestModifyView<DashboardResource, Input> {
DashboardResource target = null;
if (input.id != null) {
try {
target = dashboards.parse(new ProjectResource(ctl), input.id);
target = dashboards.parse(
new ProjectResource(ctl),
IdString.fromUrl(input.id));
} catch (ResourceNotFoundException e) {
throw new BadRequestException("dashboard " + input.id + " not found");
}