org.apache.commons.io.Charsets Java Examples

The following examples show how to use org.apache.commons.io.Charsets. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SingleFileScriptArchiveTest.java    From Nicobar with Apache License 2.0 6 votes vote down vote up
@Test
public void testDefaultModuleSpec() throws Exception {
    URL rootPathUrl = getClass().getClassLoader().getResource(TEST_SCRIPTS_PATH.getResourcePath());
    Path rootPath = Paths.get(rootPathUrl.toURI()).toAbsolutePath();
    Set<String> singleFileScripts = TEST_SCRIPTS_PATH.getContentPaths();
    for (String script: singleFileScripts) {
        Path scriptPath = rootPath.resolve(script);
        SingleFileScriptArchive scriptArchive = new SingleFileScriptArchive.Builder(scriptPath)
            .build();
        String moduleId = script.replaceAll("\\.", "_");
        assertEquals(scriptArchive.getModuleSpec().getModuleId().toString(), moduleId);
        Set<String> archiveEntryNames = scriptArchive.getArchiveEntryNames();
        assertEquals(archiveEntryNames.size(), 1);
        for (String entryName : archiveEntryNames) {
            URL entryUrl = scriptArchive.getEntry(entryName);
            assertNotNull(entryUrl);
            InputStream inputStream = entryUrl.openStream();
            String content = IOUtils.toString(inputStream, Charsets.UTF_8);

            // We have stored the file name as the content of the file
            assertEquals(content, script + "\n");
        }
    }
}
 
Example #2
Source File: HttpServerTest.java    From msf4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testUploadReject() throws Exception {
    HttpURLConnection urlConn = request("/test/v1/uploadReject", HttpMethod.POST, true);
    try {
        urlConn.setChunkedStreamingMode(1024);
        urlConn.getOutputStream().write("Rejected Content".getBytes(Charsets.UTF_8));
        try {
            urlConn.getInputStream();
            fail();
        } catch (IOException e) {
            // Expect to get exception since server response with 400. Just drain the error stream.
            IOUtils.toByteArray(urlConn.getErrorStream());
            assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), urlConn.getResponseCode());
        }
    } finally {
        urlConn.disconnect();
    }
}
 
Example #3
Source File: LdapGroupsMapping.java    From big-c with Apache License 2.0 6 votes vote down vote up
String extractPassword(String pwFile) {
  if (pwFile.isEmpty()) {
    // If there is no password file defined, we'll assume that we should do
    // an anonymous bind
    return "";
  }

  StringBuilder password = new StringBuilder();
  try (Reader reader = new InputStreamReader(
      new FileInputStream(pwFile), Charsets.UTF_8)) {
    int c = reader.read();
    while (c > -1) {
      password.append((char)c);
      c = reader.read();
    }
    return password.toString().trim();
  } catch (IOException ioe) {
    throw new RuntimeException("Could not read password file: " + pwFile, ioe);
  }
}
 
Example #4
Source File: Display.java    From big-c with Apache License 2.0 6 votes vote down vote up
/**
 * Read a single byte from the stream.
 */
@Override
public int read() throws IOException {
  if (pos < buffer.length) {
    return buffer[pos++];
  }
  if (!fileReader.hasNext()) {
    return -1;
  }
  writer.write(fileReader.next(), encoder);
  encoder.flush();
  if (!fileReader.hasNext()) {
    // Write a new line after the last Avro record.
    output.write(System.getProperty("line.separator")
                     .getBytes(Charsets.UTF_8));
    output.flush();
  }
  pos = 0;
  buffer = output.toByteArray();
  output.reset();
  return read();
}
 
Example #5
Source File: OAuthUtil.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * Generates a random number using two UUIDs and HMAC-SHA1
 *
 * @return generated secure random number
 * @throws IdentityOAuthAdminException Invalid Algorithm or Invalid Key
 */
public static String getRandomNumber() throws IdentityOAuthAdminException {
    try {
        String secretKey = UUIDGenerator.generateUUID();
        String baseString = UUIDGenerator.generateUUID();

        SecretKeySpec key = new SecretKeySpec(secretKey.getBytes(Charsets.UTF_8), ALGORITHM);
        Mac mac = Mac.getInstance(ALGORITHM);
        mac.init(key);
        byte[] rawHmac = mac.doFinal(baseString.getBytes(Charsets.UTF_8));
        String random = Base64.encode(rawHmac);
        // Registry doesn't have support for these character.
        random = random.replace("/", "_");
        random = random.replace("=", "a");
        random = random.replace("+", "f");
        return random;
    } catch (Exception e) {
        throw new IdentityOAuthAdminException("Error when generating a random number.", e);
    }
}
 
Example #6
Source File: XmlCollectionWithTagInputFormat.java    From vxquery with Apache License 2.0 6 votes vote down vote up
public XmlRecordReader(FileSplit split, Configuration conf) throws IOException {
    endTag = ENDING_TAG.getBytes(Charsets.UTF_8);
    startTag = STARTING_TAG.getBytes(Charsets.UTF_8);

    // open the file and seek to the start of the split
    start = split.getStart();
    // set the end of the file
    end = start + split.getLength();
    Path file = split.getPath();
    FileSystem fs = file.getFileSystem(conf);
    FileStatus fStatus = fs.getFileStatus(file);
    blocks = fs.getFileBlockLocations(fStatus, 0, fStatus.getLen());
    // seek the start of file
    fsin = fs.open(split.getPath());
    fsin.seek(start);
}
 
Example #7
Source File: Uri.java    From AndServer with Apache License 2.0 6 votes vote down vote up
private static MultiValueMap<String, String> convertQuery(String query) {
    MultiValueMap<String, String> valueMap = new LinkedMultiValueMap<>();
    if (!StringUtils.isEmpty(query)) {
        if (query.startsWith("?")) query = query.substring(1);

        StringTokenizer tokenizer = new StringTokenizer(query, "&");
        while (tokenizer.hasMoreElements()) {
            String element = tokenizer.nextToken();
            int end = element.indexOf("=");

            if (end > 0 && end < element.length() - 1) {
                String key = element.substring(0, end);
                String value = element.substring(end + 1);
                valueMap.add(key, UrlCoder.urlDecode(value, Charsets.UTF_8));
            }
        }
    }
    return valueMap;
}
 
Example #8
Source File: DataBridgeManifestReader.java    From herd with Apache License 2.0 6 votes vote down vote up
/**
 * Reads a JSON manifest file into a JSON manifest object.
 *
 * @param jsonManifestFile the JSON manifest file.
 *
 * @return the manifest object.
 * @throws java.io.IOException if any errors were encountered reading the JSON file.
 * @throws IllegalArgumentException if the manifest file has validation errors.
 */
public M readJsonManifest(File jsonManifestFile) throws IOException, IllegalArgumentException
{
    // Verify that the file exists and can be read.
    HerdFileUtils.verifyFileExistsAndReadable(jsonManifestFile);

    // Deserialize the JSON manifest.
    BufferedInputStream buffer = new BufferedInputStream(new FileInputStream(jsonManifestFile));
    BufferedReader reader = new BufferedReader(new InputStreamReader(buffer, Charsets.UTF_8));
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
    objectMapper.setVisibility(PropertyAccessor.GETTER, Visibility.NONE);
    objectMapper.setVisibility(PropertyAccessor.SETTER, Visibility.NONE);
    M manifest = getManifestFromReader(reader, objectMapper);

    // Validate the manifest and return it.
    validateManifest(manifest);
    return manifest;
}
 
Example #9
Source File: MultipartFormDataParserTestCase.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
@Test
public void testFileUploadWithEagerParsingAndNonASCIIFilename() throws Exception {
    DefaultServer.setRootHandler(new EagerFormParsingHandler().setNext(createHandler()));
    TestHttpClient client = new TestHttpClient();
    try {

        HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/path");
        MultipartEntity entity = new MultipartEntity();

        entity.addPart("formValue", new StringBody("myValue", "text/plain", StandardCharsets.UTF_8));

        File uploadfile = new File(MultipartFormDataParserTestCase.class.getResource("uploadfile.txt").getFile());
        FormBodyPart filePart = new FormBodyPart("file", new FileBody(uploadfile, "τεστ", "application/octet-stream", Charsets.UTF_8.toString()));
        filePart.addField("Content-Disposition", "form-data; name=\"file\"; filename*=\"utf-8''%CF%84%CE%B5%CF%83%CF%84.txt\"");
        entity.addPart(filePart);

        post.setEntity(entity);
        HttpResponse result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);


    } finally {
        client.getConnectionManager().shutdown();
    }
}
 
Example #10
Source File: ConsumeWindowsEventLogTest.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
public static List<WinNT.HANDLE> mockEventHandles(WEvtApi wEvtApi, Kernel32 kernel32, List<String> eventXmls) {
    List<WinNT.HANDLE> eventHandles = new ArrayList<>();
    for (String eventXml : eventXmls) {
        WinNT.HANDLE eventHandle = mock(WinNT.HANDLE.class);
        when(wEvtApi.EvtRender(isNull(WinNT.HANDLE.class), eq(eventHandle), eq(WEvtApi.EvtRenderFlags.EVENT_XML),
                anyInt(), any(Pointer.class), any(Pointer.class), any(Pointer.class))).thenAnswer(invocation -> {
            Object[] arguments = invocation.getArguments();
            Pointer bufferUsed = (Pointer) arguments[5];
            byte[] array = Charsets.UTF_16LE.encode(eventXml).array();
            if (array.length > (int) arguments[3]) {
                when(kernel32.GetLastError()).thenReturn(W32Errors.ERROR_INSUFFICIENT_BUFFER).thenReturn(W32Errors.ERROR_SUCCESS);
            } else {
                ((Pointer) arguments[4]).write(0, array, 0, array.length);
            }
            bufferUsed.setInt(0, array.length);
            return false;
        });
        eventHandles.add(eventHandle);
    }
    return eventHandles;
}
 
Example #11
Source File: LdapGroupsMapping.java    From hadoop with Apache License 2.0 6 votes vote down vote up
String extractPassword(String pwFile) {
  if (pwFile.isEmpty()) {
    // If there is no password file defined, we'll assume that we should do
    // an anonymous bind
    return "";
  }

  StringBuilder password = new StringBuilder();
  try (Reader reader = new InputStreamReader(
      new FileInputStream(pwFile), Charsets.UTF_8)) {
    int c = reader.read();
    while (c > -1) {
      password.append((char)c);
      c = reader.read();
    }
    return password.toString().trim();
  } catch (IOException ioe) {
    throw new RuntimeException("Could not read password file: " + pwFile, ioe);
  }
}
 
Example #12
Source File: SshProviderMockTest.java    From parallec with Apache License 2.0 6 votes vote down vote up
@Test
public void executeAndGenResponseThrowsExceptionTest() {

    // ResponseOnSingeReq (Channel channel) {
    ResponseOnSingeRequest sshResponse = new ResponseOnSingeRequest();
    ChannelExec channel = mock(ChannelExec.class);
    sshProvider = new SshProvider(sshMetaKey, hostIpSample);
    String stdoutStr = "Mon Sep 14 21:52:27 UTC 2015";
    InputStream in = new ByteArrayInputStream(
            stdoutStr.getBytes(Charsets.UTF_8));

    try {
        when(channel.getInputStream()).thenReturn(in);
        when(channel.isClosed()).thenReturn(false).thenThrow(
                new RuntimeException("fake exception"));
        sshResponse = sshProvider.executeAndGenResponse(channel);
    } catch (Throwable e) {
        logger.info("expected exception {}", "String", e);
    }

    logger.info(sshResponse.toString());
}
 
Example #13
Source File: ConsumeWindowsEventLog.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
private void processQueue(ProcessSession session) {
    String xml;
    while ((xml = renderedXMLs.peek()) != null) {
        FlowFile flowFile = session.create();
        byte[] xmlBytes = xml.getBytes(Charsets.UTF_8);
        flowFile = session.write(flowFile, out -> out.write(xmlBytes));
        flowFile = session.putAttribute(flowFile, CoreAttributes.MIME_TYPE.key(), APPLICATION_XML);
        session.getProvenanceReporter().receive(flowFile, provenanceUri);
        session.transfer(flowFile, REL_SUCCESS);
        session.commit();
        if (!renderedXMLs.remove(xml) && getLogger().isWarnEnabled()) {
            getLogger().warn(new StringBuilder("Event ")
                    .append(xml)
                    .append(" had already been removed from queue, FlowFile ")
                    .append(flowFile.getAttribute(CoreAttributes.UUID.key()))
                    .append(" possible duplication of data")
                    .toString());
        }
    }
}
 
Example #14
Source File: JinjaTemplatesTest.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
public String jobRunJinjaTemplateTest(Boolean dist, Boolean launchMode) throws IOException {
  URL urlTemplate = Resources.getResource(SubmarineJob.SUBMARINE_JOBRUN_TF_JINJA);
  String template = Resources.toString(urlTemplate, Charsets.UTF_8);
  Jinjava jinjava = new Jinjava();
  HashMap<String, Object> jinjaParams = initJinjaParams(dist, launchMode);

  String submarineCmd = jinjava.render(template, jinjaParams);
  int pos = submarineCmd.indexOf("\n");
  if (pos == 0) {
    submarineCmd = submarineCmd.replaceFirst("\n", "");
  }

  LOGGER.info("------------------------");
  LOGGER.info(submarineCmd);
  LOGGER.info("------------------------");

  return submarineCmd;
}
 
Example #15
Source File: DhcpTest.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Test option with option length > 128.
 */
@Test
public void longOptionTest() throws Exception {
    byte[] data = Resources.toByteArray(Dhcp6RelayTest.class.getResource(LONG_OPT));
    DHCP dhcp = DHCP.deserializer().deserialize(data, 0, data.length);
    assertEquals(2, dhcp.getOptions().size());
    DhcpOption hostnameOption = dhcp.getOption(DHCP.DHCPOptionCode.OptionCode_HostName);
    DhcpOption endOption = dhcp.getOption(DHCP.DHCPOptionCode.OptionCode_END);
    assertNotNull(hostnameOption);
    assertNotNull(endOption);

    // Host name contains 200 "A"
    StringBuilder hostnameBuilder = new StringBuilder();
    IntStream.range(0, 200).forEach(i -> hostnameBuilder.append("A"));
    String hostname = hostnameBuilder.toString();

    assertEquals((byte) 200, hostnameOption.getLength());
    assertArrayEquals(hostname.getBytes(Charsets.US_ASCII), hostnameOption.getData());
}
 
Example #16
Source File: JinjaTemplatesTest.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
public String tensorboardJinjaTemplateTest(Boolean dist, Boolean launchMode) throws IOException {
  URL urlTemplate = Resources.getResource(SubmarineJob.SUBMARINE_TENSORBOARD_JINJA);
  String template = Resources.toString(urlTemplate, Charsets.UTF_8);
  Jinjava jinjava = new Jinjava();
  HashMap<String, Object> jinjaParams = initJinjaParams(dist, launchMode);

  String submarineCmd = jinjava.render(template, jinjaParams);
  int pos = submarineCmd.indexOf("\n");
  if (pos == 0) {
    submarineCmd = submarineCmd.replaceFirst("\n", "");
  }
  LOGGER.info("------------------------");
  LOGGER.info(submarineCmd);
  LOGGER.info("------------------------");

  return submarineCmd;
}
 
Example #17
Source File: GolangHostedFacetImplTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void getList() throws IOException {
  String expected = "v1.0.0\nv1.0.1";
  String name = "module/@v/v1.0.0.list";

  NestedAttributesMap attributesMap = mock(NestedAttributesMap.class);
  when(attributesMap.get("asset_kind")).thenReturn(PACKAGE.name());
  when(asset1.name()).thenReturn("modulename/@v/v1.0.0.zip");
  when(asset1.formatAttributes()).thenReturn(attributesMap);
  when(asset2.name()).thenReturn("modulename/@v/v1.0.1.zip");
  when(asset2.formatAttributes()).thenReturn(attributesMap);

  when(dataAccess.findAssetsForModule(tx, repository, name)).thenReturn(
      ImmutableList.of(asset1, asset2)
  );

  Content content = underTest.getList(name);

  String response = CharStreams.toString(new InputStreamReader(content.openInputStream(), Charsets.UTF_8));

  assertThat(response, is(equalTo(expected)));
}
 
Example #18
Source File: FSDirMkdirOp.java    From big-c with Apache License 2.0 6 votes vote down vote up
/**
 * For a given absolute path, create all ancestors as directories along the
 * path. All ancestors inherit their parent's permission plus an implicit
 * u+wx permission. This is used by create() and addSymlink() for
 * implicitly creating all directories along the path.
 *
 * For example, path="/foo/bar/spam", "/foo" is an existing directory,
 * "/foo/bar" is not existing yet, the function will create directory bar.
 *
 * @return a tuple which contains both the new INodesInPath (with all the
 * existing and newly created directories) and the last component in the
 * relative path. Or return null if there are errors.
 */
static Map.Entry<INodesInPath, String> createAncestorDirectories(
    FSDirectory fsd, INodesInPath iip, PermissionStatus permission)
    throws IOException {
  final String last = new String(iip.getLastLocalName(), Charsets.UTF_8);
  INodesInPath existing = iip.getExistingINodes();
  List<String> children = iip.getPath(existing.length(),
      iip.length() - existing.length());
  int size = children.size();
  if (size > 1) { // otherwise all ancestors have been created
    List<String> directories = children.subList(0, size - 1);
    INode parentINode = existing.getLastINode();
    // Ensure that the user can traversal the path by adding implicit
    // u+wx permission to all ancestor directories
    existing = createChildrenDirectories(fsd, existing, directories,
        addImplicitUwx(parentINode.getPermissionStatus(), permission));
    if (existing == null) {
      return null;
    }
  }
  return new AbstractMap.SimpleImmutableEntry<>(existing, last);
}
 
Example #19
Source File: HyperParameterScopeConfigReader.java    From laozhongyi with MIT License 6 votes vote down vote up
public static List<HyperParameterScopeItem> read(final String configFilePath) {
    final File file = new File(configFilePath);
    List<String> lines;
    try {
        lines = FileUtils.readLines(file, Charsets.UTF_8);
    } catch (final IOException e) {
        throw new IllegalStateException(e);
    }
    final List<HyperParameterScopeItem> result = Lists.newArrayList();
    for (final String line : lines) {
        final String[] segments = StringUtils.split(line, ',');
        final List<String> values = Lists.newArrayList();
        for (int i = 1; i < segments.length; ++i) {
            values.add(segments[i]);
        }
        final HyperParameterScopeItem item = new HyperParameterScopeItem(segments[0], values);
        result.add(item);
    }
    return result;
}
 
Example #20
Source File: AbstractAWSGatewayApiProcessor.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a map of Query Parameter Name to Values
 *
 * @param context ProcessContext
 * @return Map of names and values
 */
protected Map<String, List<String>> getParameters(ProcessContext context) {

    if (!context.getProperty(PROP_QUERY_PARAMS).isSet()) {
        return new HashMap<>();
    }
    final String queryString = context.getProperty(PROP_QUERY_PARAMS)
                                      .evaluateAttributeExpressions().getValue();
    List<NameValuePair> params = URLEncodedUtils
        .parse(queryString, Charsets.toCharset("UTF-8"));

    if (params.isEmpty()) {
        return new HashMap<>();
    }

    Map<String, List<String>> map = new HashMap<>();

    for (NameValuePair nvp : params) {
        if (!map.containsKey(nvp.getName())) {
            map.put(nvp.getName(), new ArrayList<>());
        }
        map.get(nvp.getName()).add(nvp.getValue());
    }
    return map;
}
 
Example #21
Source File: TestGetBlockLocations.java    From hadoop with Apache License 2.0 6 votes vote down vote up
private static FSNamesystem setupFileSystem() throws IOException {
  Configuration conf = new Configuration();
  conf.setLong(DFS_NAMENODE_ACCESSTIME_PRECISION_KEY, 1L);
  FSEditLog editlog = mock(FSEditLog.class);
  FSImage image = mock(FSImage.class);
  when(image.getEditLog()).thenReturn(editlog);
  final FSNamesystem fsn = new FSNamesystem(conf, image, true);

  final FSDirectory fsd = fsn.getFSDirectory();
  INodesInPath iip = fsd.getINodesInPath("/", true);
  PermissionStatus perm = new PermissionStatus(
      "hdfs", "supergroup",
      FsPermission.createImmutable((short) 0x1ff));
  final INodeFile file = new INodeFile(
      MOCK_INODE_ID, FILE_NAME.getBytes(Charsets.UTF_8),
      perm, 1, 1, new BlockInfoContiguous[] {}, (short) 1,
      DFS_BLOCK_SIZE_DEFAULT);
  fsn.getFSDirectory().addINode(iip, file);
  return fsn;
}
 
Example #22
Source File: GolangHostedFacetImplTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void getInfo() throws IOException {
  DateTime blobCreated = DateTime.now();
  String expected = String.format("{\"Version\":\"v1.0.0\",\"Time\":\"%s\"}", blobCreated.toString());
  when(dataAccess.findAsset(any(), any(), any())).thenReturn(asset1);
  when(asset1.blobCreated()).thenReturn(blobCreated);

  GolangAttributes goAttributes = new GolangAttributes();
  goAttributes.setModule("module");
  goAttributes.setVersion("v1.0.0");

  Content content = underTest.getInfo(MODULE_INFO, goAttributes);
  String response = CharStreams.toString(new InputStreamReader(content.openInputStream(), Charsets.UTF_8));

  assertThat(response, is(equalTo(expected)));
}
 
Example #23
Source File: FSDirMkdirOp.java    From hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * For a given absolute path, create all ancestors as directories along the
 * path. All ancestors inherit their parent's permission plus an implicit
 * u+wx permission. This is used by create() and addSymlink() for
 * implicitly creating all directories along the path.
 *
 * For example, path="/foo/bar/spam", "/foo" is an existing directory,
 * "/foo/bar" is not existing yet, the function will create directory bar.
 *
 * @return a tuple which contains both the new INodesInPath (with all the
 * existing and newly created directories) and the last component in the
 * relative path. Or return null if there are errors.
 */
static Map.Entry<INodesInPath, String> createAncestorDirectories(
    FSDirectory fsd, INodesInPath iip, PermissionStatus permission)
    throws IOException {
  final String last = new String(iip.getLastLocalName(), Charsets.UTF_8);
  INodesInPath existing = iip.getExistingINodes();
  List<String> children = iip.getPath(existing.length(),
      iip.length() - existing.length());
  int size = children.size();
  if (size > 1) { // otherwise all ancestors have been created
    List<String> directories = children.subList(0, size - 1);
    INode parentINode = existing.getLastINode();
    // Ensure that the user can traversal the path by adding implicit
    // u+wx permission to all ancestor directories
    existing = createChildrenDirectories(fsd, existing, directories,
        addImplicitUwx(parentINode.getPermissionStatus(), permission));
    if (existing == null) {
      return null;
    }
  }
  return new AbstractMap.SimpleImmutableEntry<>(existing, last);
}
 
Example #24
Source File: PolicyEditorService.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves a Policy document from a given URL
 *
 * @param policyURL
 * @return A CDATA Wrapped Policy document if found
 * @throws AxisFault
 */
public String getPolicyDoc(String policyURL) throws AxisFault {
    String policy = "";

    // Open a stream to the policy file using the URL.
    try {
        URL url = new URL(policyURL);

        InputStream in = url.openStream();
        BufferedReader dis =
                new BufferedReader(new InputStreamReader(in, Charsets.UTF_8));
        StringBuilder fBuf = new StringBuilder();

        String line = "";
        while ((line = dis.readLine()) != null) {
            fBuf.append(line).append("\n");
        }
        in.close();

        policy = fBuf.toString();
        dis.close();
    } catch (IOException e) {
        throw new AxisFault("Axis Error while getting policy docs." ,e);
    }

    return "<![CDATA[" + policy + "]]>";
}
 
Example #25
Source File: JsonMessageModule.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * To get Base64 encoded username:password for basic authentication header
 *
 * @param username username
 * @param password password
 * @return Base64 encoded value of username:password
 */
private String getBase64EncodedBasicAuthHeader(String username, String password) {

    String concatenatedCredential = username + ":" + password;
    byte[] byteValue = concatenatedCredential.getBytes(Charsets.UTF_8);
    String encodedAuthHeader = Base64.encode(byteValue);
    encodedAuthHeader = "Basic " + encodedAuthHeader;
    return encodedAuthHeader;
}
 
Example #26
Source File: UserProvider.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Override
public CredentialEntry getCredentialEntry(String alias) {
  byte[] bytes = credentials.getSecretKey(new Text(alias));
  if (bytes == null) {
    return null;
  }
  return new CredentialEntry(
      alias, new String(bytes, Charsets.UTF_8).toCharArray());
}
 
Example #27
Source File: KeyProvider.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Serialize the metadata to a set of bytes.
 * @return the serialized bytes
 * @throws IOException
 */
protected byte[] serialize() throws IOException {
  ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  JsonWriter writer = new JsonWriter(
      new OutputStreamWriter(buffer, Charsets.UTF_8));
  try {
    writer.beginObject();
    if (cipher != null) {
      writer.name(CIPHER_FIELD).value(cipher);
    }
    if (bitLength != 0) {
      writer.name(BIT_LENGTH_FIELD).value(bitLength);
    }
    if (created != null) {
      writer.name(CREATED_FIELD).value(created.getTime());
    }
    if (description != null) {
      writer.name(DESCRIPTION_FIELD).value(description);
    }
    if (attributes != null && attributes.size() > 0) {
      writer.name(ATTRIBUTES_FIELD).beginObject();
      for (Map.Entry<String, String> attribute : attributes.entrySet()) {
        writer.name(attribute.getKey()).value(attribute.getValue());
      }
      writer.endObject();
    }
    writer.name(VERSIONS_FIELD).value(versions);
    writer.endObject();
    writer.flush();
  } finally {
    writer.close();
  }
  return buffer.toByteArray();
}
 
Example #28
Source File: TestTCPServerSource.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
  ByteBuf buf = (ByteBuf) msg;
  final String readMsg = buf.toString(com.google.common.base.Charsets.UTF_8);
  LOG.debug("Channel read message: {}", readMsg);
  final List<String> acks = Arrays.asList(readMsg.split(ACK_SEPARATOR));
  LOG.debug("Split into acks: {}", acks);
  this.acks.addAll(acks);
}
 
Example #29
Source File: CREATE3Request.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(XDR xdr) {
  handle.serialize(xdr);
  xdr.writeInt(name.length());
  xdr.writeFixedOpaque(name.getBytes(Charsets.UTF_8), name.length());
  xdr.writeInt(mode);
  objAttr.serialize(xdr);
}
 
Example #30
Source File: LINK3Request.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(XDR xdr) {
  handle.serialize(xdr);
  fromDirHandle.serialize(xdr);
  xdr.writeInt(fromName.length());
  xdr.writeFixedOpaque(fromName.getBytes(Charsets.UTF_8), fromName.length());
}