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