001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2012-2016 ForgeRock AS.
015 */
016package org.opends.dsml.protocol;
017
018import java.io.IOException;
019import java.io.InputStream;
020import java.net.URI;
021
022import org.forgerock.opendj.ldap.ByteString;
023import org.forgerock.opendj.ldap.ByteStringBuilder;
024import org.w3c.dom.Element;
025
026/**
027 * A utility class to assist in converting DsmlValues (in Objects) into
028 * the required ByteStrings, and back again.
029 */
030public class ByteStringUtility
031{
032  /**
033   * Returns a ByteString from a DsmlValue Object.
034   *
035   * @param obj
036   *           the DsmlValue object.
037   * @return a new ByteString object with the value, or null if val was null,
038   *         or if it could not be converted.
039   * @throws IOException if any problems occurred retrieving an anyURI value.
040   */
041  public static ByteString convertValue(Object obj) throws IOException
042  {
043    if (obj == null)
044    {
045      return null;
046    }
047    else if (obj instanceof String)
048    {
049      return ByteString.valueOfUtf8((String) obj);
050    }
051    else if (obj instanceof byte[])
052    {
053      return ByteString.wrap((byte[]) obj);
054    }
055    else if (obj instanceof URI)
056    {
057      // read raw content and return as a byte[].
058      try (InputStream is = ((URI) obj).toURL().openStream())
059      {
060        ByteStringBuilder bsb = new ByteStringBuilder();
061        while (bsb.appendBytes(is, 2048) != -1)
062        {
063          // do nothing
064        }
065        return bsb.toByteString();
066      }
067    }
068    else if (obj instanceof Element)
069    {
070      Element element = (Element) obj;
071      return ByteString.valueOfUtf8(element.getTextContent());
072    }
073    return null;
074  }
075
076  /**
077   * Returns a DsmlValue (Object) from an LDAP ByteString. The conversion is
078   * simplistic - try and convert it to UTF-8 and if that fails return a byte[].
079   *
080   * @param bs the ByteString returned from LDAP.
081   * @return a String or a byte[].
082   */
083  public static Object convertByteString(ByteString bs)
084  {
085    try
086    {
087      return new String(bs.toCharArray());
088    }
089    catch (Exception e)
090    {
091      return bs.toByteArray();
092    }
093  }
094}