Juan Hernandez has uploaded a new change for review. Change subject: restapi: Add HexUtils ......................................................................
restapi: Add HexUtils This patch adds a utility class used to convert hexadecimal identifiers to strings and the other way around. It will be used by resources that don't have a identifier, so the identifier will be generated as the hex encoding of the name. Change-Id: I703763e11d26f2a1a0c6b0984249acc8a6744f95 Related-To: https://bugzilla.redhat.com/1132259 Signed-off-by: Juan Hernandez <[email protected]> --- A backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/utils/HexUtils.java M backend/manager/modules/restapi/types/src/main/modules/org/ovirt/engine/api/restapi-types/main/module.xml 2 files changed, 44 insertions(+), 0 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/68/33968/1 diff --git a/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/utils/HexUtils.java b/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/utils/HexUtils.java new file mode 100644 index 0000000..d3aa482 --- /dev/null +++ b/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/utils/HexUtils.java @@ -0,0 +1,43 @@ +/* +* Copyright (c) 2014 Red Hat, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +package org.ovirt.engine.api.restapi.utils; + +import java.nio.charset.Charset; + +import org.apache.commons.codec.DecoderException; +import org.apache.commons.codec.binary.Hex; + +public class HexUtils { + private static final Charset UTF_8 = Charset.forName("UTF-8"); + + public static String string2hex(String text) { + return Hex.encodeHexString(text.getBytes(UTF_8)); + } + + private static RuntimeException notValidHex(String hex, Exception exception) { + return new IllegalArgumentException("The string \"" + hex + "\" isn't a valid hexadecimal value", exception); + } + + public static String hex2string(String hex) { + try { + return new String(Hex.decodeHex(hex.toCharArray()), UTF_8); + } + catch (DecoderException exception) { + throw notValidHex(hex, exception); + } + } +} diff --git a/backend/manager/modules/restapi/types/src/main/modules/org/ovirt/engine/api/restapi-types/main/module.xml b/backend/manager/modules/restapi/types/src/main/modules/org/ovirt/engine/api/restapi-types/main/module.xml index 04ec031..c9ff57f 100644 --- a/backend/manager/modules/restapi/types/src/main/modules/org/ovirt/engine/api/restapi-types/main/module.xml +++ b/backend/manager/modules/restapi/types/src/main/modules/org/ovirt/engine/api/restapi-types/main/module.xml @@ -25,6 +25,7 @@ <dependencies> <module name="javax.api"/> <module name="javax.xml.bind.api"/> + <module name="org.apache.commons.codec"/> <module name="org.apache.commons.lang"/> <module name="org.ovirt.engine.api.interface-common-jaxrs"/> <module name="org.ovirt.engine.api.restapi-definition"/> -- To view, visit http://gerrit.ovirt.org/33968 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I703763e11d26f2a1a0c6b0984249acc8a6744f95 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Juan Hernandez <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
