org.apache.commons.codec.Charsets Java Examples

The following examples show how to use org.apache.commons.codec.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: HttpClientSourceIT.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
@POST
public Response post(
    @Context HttpHeaders h,
    @FormParam(OAuth2ConfigBean.GRANT_TYPE_KEY) String type
) {
  String[] creds = new String(
      Base64.decodeBase64(h.getHeaderString(HttpHeaders.AUTHORIZATION).substring("Basic ".length())), Charsets.UTF_8)
      .split(":");
  if (creds.length == 2 && creds[0].equals(CLIENT_ID) && creds[1].equals(CLIENT_SECRET)) {
    token = RandomStringUtils.randomAlphanumeric(16);
    String tokenResponse = "{\n" +
        "  \"token_type\": \"Bearer\",\n" +
        "  \"expires_in\": \"3600\",\n" +
        "  \"ext_expires_in\": \"0\",\n" +
        "  \"expires_on\": \"1484788319\",\n" +
        "  \"not_before\": \"1484784419\",\n" +
        "  \"access_token\": \"" + token + "\"\n" +
        "}";
    tokenGetCount++;
    return Response.ok().entity(tokenResponse).build();
  }
  return Response.status(Response.Status.FORBIDDEN).build();
}
 
Example #2
Source File: UserController.java    From SpringBlade with Apache License 2.0 6 votes vote down vote up
/**
 * 导出用户
 */
@SneakyThrows
@GetMapping("export-user")
@ApiOperationSupport(order = 13)
@ApiOperation(value = "导出用户", notes = "传入user")
public void exportUser(@ApiIgnore @RequestParam Map<String, Object> user, BladeUser bladeUser, HttpServletResponse response) {
	QueryWrapper<User> queryWrapper = Condition.getQueryWrapper(user, User.class);
	if (!SecureUtil.isAdministrator()){
		queryWrapper.lambda().eq(User::getTenantId, bladeUser.getTenantId());
	}
	queryWrapper.lambda().eq(User::getIsDeleted, BladeConstant.DB_NOT_DELETED);
	List<UserExcel> list = userService.exportUser(queryWrapper);
	response.setContentType("application/vnd.ms-excel");
	response.setCharacterEncoding(Charsets.UTF_8.name());
	String fileName = URLEncoder.encode("用户数据导出", Charsets.UTF_8.name());
	response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
	EasyExcel.write(response.getOutputStream(), UserExcel.class).sheet("用户数据表").doWrite(list);
}
 
Example #3
Source File: RealmsAPI.java    From FishingBot with GNU General Public License v3.0 6 votes vote down vote up
public String getServerIP(long serverId) {
    HttpUriRequest request = RequestBuilder.get()
            .setUri(REALMS_ENDPOINT + "/worlds/v1/" + serverId + "/join/pc")
            .setHeader(HttpHeaders.CONTENT_TYPE, "application/json")
            .build();

    try {
        HttpResponse answer = client.execute(request);
        if (answer.getStatusLine().getStatusCode() != 200) {
            FishingBot.getLog().severe("Could not retrieve IP from " + REALMS_ENDPOINT + ": " + answer.getStatusLine());
            return null;
        }
        JsonObject responseJson = (JsonObject) new JsonParser().parse(EntityUtils.toString(answer.getEntity(), Charsets.UTF_8));
        FishingBot.getLog().info("Connecting to: " + responseJson.toString());
        return responseJson.get("address").getAsString();
    } catch (IOException e) {
        FishingBot.getLog().severe("Could not connect to " + REALMS_ENDPOINT);
    }
    return null;
}
 
Example #4
Source File: SentrySingletonTestInstance.java    From incubator-sentry with Apache License 2.0 6 votes vote down vote up
public void setupSentry() throws Exception {
  sentrySite = File.createTempFile("sentry-site", "xml");
  File authProviderDir = SolrTestCaseJ4.getFile("sentry-handlers/sentry");
  sentrySite.deleteOnExit();

  // need to write sentry-site at execution time because we don't know
  // the location of sentry.solr.provider.resource beforehand
  StringBuilder sentrySiteData = new StringBuilder();
  sentrySiteData.append("<configuration>\n");
  addPropertyToSentry(sentrySiteData, "sentry.provider",
    "org.apache.sentry.provider.file.LocalGroupResourceAuthorizationProvider");
  addPropertyToSentry(sentrySiteData, "sentry.solr.provider.resource",
     new File(authProviderDir.toString(), "test-authz-provider.ini").toURI().toURL().toString());
  sentrySiteData.append("</configuration>\n");
  FileUtils.writeStringToFile(sentrySite,sentrySiteData.toString(), Charsets.UTF_8.toString());
}
 
Example #5
Source File: RepositoryControllerTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void putOnExistingRepoFails() throws Exception {
	request.setMethod(HttpMethod.PUT.name());
	request.setContentType(RDFFormat.NTRIPLES.getDefaultMIMEType());
	request.setContent(
			("_:node1 <" + RepositoryConfigSchema.REPOSITORYID + "> \"" + repositoryId + "\" .")
					.getBytes(Charsets.UTF_8));
	when(manager.hasRepositoryConfig(repositoryId)).thenReturn(true);

	try {
		controller.handleRequest(request, response);
		fail("expected exception");
	} catch (ClientHTTPException e) {
		assertThat(e.getStatusCode()).isEqualTo(409);
	}
}
 
Example #6
Source File: JsonConfigReaderTest.java    From JerryMouse with MIT License 5 votes vote down vote up
@Test
public void parseStringAsConfig() throws Exception {
    JsonConfigReader reader = new JsonConfigReader();
    String raw = reader.readAsString("config.json", Charsets.UTF_8);
    Configuration actual = reader.parseStringAsConfiguration(raw);
    Map<String, ServletWrapper> router = new HashMap<>();
    ServletWrapper wrapper1 = new ServletWrapper("index", "/", "xxx.xxxx.xxxxx.IndexServlet", 0, null,null);
    ServletWrapper wrapper2 = new ServletWrapper("reg", "/reg", "xxx.xxxx.xxxxx.RegServlet", null, null,null);
    router.put("/", wrapper1);
    router.put("/reg", wrapper2);
    Configuration expect = new Configuration(9000, 3, router);
    assertEquals(expect,actual);
}
 
Example #7
Source File: GeoTemporalKey.java    From OSTMap with Apache License 2.0 5 votes vote down vote up
/**
 * Calculates spreadingByte (hash modulo 255)
 *
 * @param json tweet's json
 * @return the calculated int value
 */
private static int calcSpreadingByte(String json) {
    int bufferSize = json.length();
    ByteBuffer bb = ByteBuffer.allocate(bufferSize);
    bb.put(json.getBytes(Charsets.UTF_8));
    return hash.hashBytes(bb.array()).asInt() % 255;
}
 
Example #8
Source File: Gobbler.java    From antsdb with GNU Lesser General Public License v3.0 5 votes vote down vote up
static MessageEntry2 alloc(SpaceManager sm, int sessionId, String message) {
    byte[] bytes = message.getBytes(Charsets.UTF_8);
    MessageEntry2 entry = new MessageEntry2(sm, bytes.length);
    entry.setSessionId(sessionId);
    Unsafe.putBytes(entry.addr + OFFSET_MESSAGE, bytes);
    entry.finish(EntryType.MESSAGE2);
    return entry;
}
 
Example #9
Source File: DataFormatPluginTest.java    From elasticsearch-dataformat with Apache License 2.0 5 votes vote down vote up
@Test
public void dumpCsvInFile() throws IOException {
    paramsCsv.put("file", csvTempFile.getAbsolutePath());

    // try-with-resources: java 7, ensure closing resources after try
    try (CurlResponse curlResponse = createRequest(node, path, paramsCsv).execute()) {
        assertAcknowledged(curlResponse, csvTempFile);
        final List<String> lines = Files.readAllLines(csvTempFile.toPath(), Charsets.UTF_8);
        assertEquals(docNumber + 1, lines.size());
        final String line = lines.get(0);
        assertLineContains(line, "\"aaa\"", "\"bbb\"", "\"ccc\"", "\"eee.fff\"", "\"eee.ggg\"", "\"eee.hhh\"");
    }
}
 
Example #10
Source File: UploadInfo.java    From tus-java-server with MIT License 5 votes vote down vote up
private String decode(String encodedValue) {
    if (encodedValue == null) {
        return null;
    } else {
        return new String(Base64.decodeBase64(encodedValue), Charsets.UTF_8);
    }
}
 
Example #11
Source File: Session.java    From antsdb with GNU Lesser General Public License v3.0 5 votes vote down vote up
public Object run(String sql, Parameters params, Consumer<Object> callback) throws SQLException {
    byte[] bytes = sql.getBytes(Charsets.UTF_8);
    ByteBuffer buf = ByteBuffer.allocateDirect(bytes.length);
    buf.put(bytes);
    buf.flip();
    return run(buf, params, callback);
}
 
Example #12
Source File: EncryptionUtility.java    From blackduck-alert with Apache License 2.0 5 votes vote down vote up
private String getEncodedSalt() {
    if (isInitialized()) {
        byte[] saltBytes = getGlobalSalt().getBytes(Charsets.UTF_8);
        return Hex.encodeHexString(saltBytes);
    } else {
        return null;
    }
}
 
Example #13
Source File: CalculateRawTwitterDataKey.java    From OSTMap with Apache License 2.0 5 votes vote down vote up
/**
 * return a int representing the hashcode of the given string
 *
 * @param json to calculate the hash for
 * @return the int hash
 */
public int getHash(String json) {
    int bufferSize = json.length();
    ByteBuffer bb = ByteBuffer.allocate(bufferSize);
    bb.put(json.getBytes(Charsets.UTF_8));
    return hash.hashBytes(bb.array()).asInt();
}
 
Example #14
Source File: MomentSketchAggregatorFactory.java    From momentsketch with Apache License 2.0 5 votes vote down vote up
@Override
public Object deserialize(Object serializedSketch) {
    if (serializedSketch instanceof String) {
        String str = (String)serializedSketch;
        return deserializeFromByteArray(Base64.decodeBase64(str.getBytes(Charsets.UTF_8)));
    } else if (serializedSketch instanceof byte[]) {
        return deserializeFromByteArray((byte[]) serializedSketch);
    } else if (serializedSketch instanceof MomentSketchWrapper) {
        return (MomentSketchWrapper) serializedSketch;
    }
    throw new ISE(
            "Object is not of a type that can be deserialized to a Moments Sketch"
                    + serializedSketch.getClass());

}
 
Example #15
Source File: Gobbler.java    From antsdb with GNU Lesser General Public License v3.0 5 votes vote down vote up
static DdlEntry alloc(SpaceManager sm, int sessionId, String message) {
    byte[] bytes = message.getBytes(Charsets.UTF_8);
    DdlEntry entry = new DdlEntry(sm, bytes.length);
    entry.setSessionId(sessionId);
    Unsafe.putBytes(entry.addr + OFFSET_DDL, bytes);
    entry.finish(EntryType.DDL);
    return entry;
}
 
Example #16
Source File: MysqlClient.java    From antsdb with GNU Lesser General Public License v3.0 5 votes vote down vote up
public PacketHandshake connect() throws IOException {
    this.ch = SocketChannel.open(new InetSocketAddress(host, port));
    this.out = new ChannelWriterNio(this.ch);
    this.encoder = new PacketEncoder(()->{ return Charsets.UTF_8;});
    readPacket();
    PacketHandshake packet = new PacketHandshake(this.buf);
    return packet;
}
 
Example #17
Source File: PacketEncoder.java    From antsdb with GNU Lesser General Public License v3.0 5 votes vote down vote up
private int getCharSetId(Charset encoder) {
    int result = MysqlConstant.MYSQL_COLLATION_INDEX_utf8_general_ci;
    if (encoder == Charsets.ISO_8859_1) {
        result = MysqlConstant.MYSQL_COLLATION_INDEX_latin1_swedish_ci;
    }
    return result;
}
 
Example #18
Source File: TestUtil.java    From github-integration-plugin with MIT License 5 votes vote down vote up
public static String classpath(Class<?> clazz, String path) {
    try {
        return IOUtils.toString(clazz.getClassLoader().getResourceAsStream(
                clazz.getName().replace(PACKAGE_SEPARATOR, File.separator) + File.separator + path
        ), Charsets.UTF_8);
    } catch (IOException e) {
        throw new RuntimeException(format("Can't load %s for class %s", path, clazz), e);
    }
}
 
Example #19
Source File: PacketEncoder.java    From antsdb with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void writeProgressBody(PacketWriter buffer, String result, Session session) {
     // header
     buffer.writeByte((byte) 0xff);
     // error code
     buffer.writeUB2(0xffff);
     // stage
     buffer.writeByte((byte)1);
     // max stage
     buffer.writeByte((byte)11);
     // progress
     buffer.writeLongInt(1);
     // message
     buffer.writeLenString(result, Charsets.UTF_8);
}
 
Example #20
Source File: ZipOutput.java    From SkyTube with GNU General Public License v3.0 5 votes vote down vote up
public void addContent(String name, String content) throws IOException {
    ZipEntry entry = new ZipEntry(name);
    outputZipStream.putNextEntry(entry);
    // TODO: After Android 4.4, we can use StandardCharsets
    outputZipStream.write(content.getBytes(Charsets.UTF_8));

    Log.d(TAG, "Added: " + name);
}
 
Example #21
Source File: S3DataManager.java    From aws-codebuild-jenkins-plugin with Apache License 2.0 4 votes vote down vote up
public static String getZipMD5(File zipFile) throws IOException {
    return new String(encodeBase64(DigestUtils.md5(new FileInputStream(zipFile))), Charsets.UTF_8);
}
 
Example #22
Source File: FinishMarker.java    From flume-plugin with Apache License 2.0 4 votes vote down vote up
private BufferedReader getReader() throws FileNotFoundException {
    return new BufferedReader(new InputStreamReader(new FileInputStream(target), Charsets.UTF_8));
}
 
Example #23
Source File: HAWK.java    From NLIWOD with GNU Affero General Public License v3.0 4 votes vote down vote up
public void search(IQuestion question, String language) throws Exception {
		String questionString;
		if (!question.getLanguageToQuestion().containsKey(language)) {
			return;
		}
		questionString = question.getLanguageToQuestion().get(language);
		log.debug(this.getClass().getSimpleName() + ": " + questionString);

		RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(this.timeout).build();
		HttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
		URIBuilder builder = new URIBuilder().setScheme("http")
				.setHost("hawk.aksw.org:8181").setPath("/search")
				.setParameter("q", questionString);
		if(this.setLangPar){
			builder = builder.setParameter("lang", language);
		}
		URI iduri = builder.build();
		HttpGet httpget = new HttpGet(iduri);
		HttpResponse idresponse = client.execute(httpget);
		
		//Test if error occured
		if(idresponse.getStatusLine().getStatusCode()>=400){
			throw new Exception("HAWK Server (ID) could not answer due to: "+idresponse.getStatusLine());
		}
		
		String id = responseparser.responseToString(idresponse);
		JSONParser parser = new JSONParser();
//http://hawk.aksw.org:8181/
		//URI quri = new URIBuilder().setScheme("http")
		//		.setHost("139.18.2.164:8181").setPath("/status")
		//		.setParameter("UUID", id.substring(1, id.length() - 2)).build();
		URI quri = new URIBuilder().setScheme("http")
				.setHost("hawk.aksw.org:8181").setPath("/status")
				.setParameter("UUID", id.substring(1, id.length() - 2)).build();

		int j = 0;
		do {
			Thread.sleep(50);
			HttpGet questionpost = new HttpGet(quri);
			HttpResponse questionresponse = client.execute(questionpost);
			
			//Test if error occured
			if(questionresponse.getStatusLine().getStatusCode()>=400){
				throw new Exception("HAWK Server (Question) could not answer due to: "+questionresponse.getStatusLine());
			}
			
			JSONObject responsejson = (JSONObject) parser.parse(responseparser
					.responseToString(questionresponse));
			if (responsejson.containsKey("answer")) {
				JSONArray answerlist = (JSONArray) responsejson.get("answer");
				HashSet<String> result = new HashSet<String>();
				for (int i = 0; i < answerlist.size(); i++) {
					JSONObject answer = (JSONObject) answerlist.get(i);
					result.add(answer.get("URI").toString());
				}
				question.setGoldenAnswers(result);
				if (responsejson.containsKey("final_sparql_base64")) {
					String sparqlQuery = responsejson
							.get("final_sparql_base64").toString();
					sparqlQuery = new String(
							based64Decoder.decode(sparqlQuery), Charsets.UTF_8);
					question.setSparqlQuery(sparqlQuery);
				}
			}
			j = j + 1;
		} while (j < 500);
	}
 
Example #24
Source File: PacketEncoder.java    From antsdb with GNU Lesser General Public License v3.0 4 votes vote down vote up
private boolean writeBinaryValueFast(PacketWriter buffer, FieldMeta meta, long pValue) {
    DataType type = meta.getType();
    byte format = Value.getFormat(null, pValue);
    if (type.getSqlType() == Types.TINYINT) {
        if (format == Value.FORMAT_INT4) {
            buffer.writeByte((byte)Int4.get(pValue));
            return true;
        }
        else if (format == Value.FORMAT_INT8) {
            buffer.writeByte((byte)Int8.get(null, pValue));
            return true;
        }
    }
    else if (type.getJavaType() == Boolean.class) {
        boolean b = FishBool.get(null, pValue);
        buffer.writeByte((byte)(b ? 1 : 0));
        return true;
    }
    else if (type.getJavaType() == Integer.class) {
        if (format == Value.FORMAT_INT4) {
            buffer.writeUB4(Int4.get(pValue));
            return true;
        }
        else if (format == Value.FORMAT_INT8) {
            buffer.writeUB4((int) Int8.get(null, pValue));
            return true;
        }
    }
    else if (type.getJavaType() == Long.class) {
        if (format == Value.FORMAT_INT4) {
            buffer.writeLongLong(Int4.get(pValue));
            return true;
        }
        else if (format == Value.FORMAT_INT8) {
            buffer.writeLongLong(Int8.get(null, pValue));
            return true;
        }
    }
    else if (type.getJavaType() == Float.class) {
        if (format == Value.FORMAT_FLOAT4) {
            buffer.writeUB4(Float.floatToIntBits(Float4.get(null, pValue)));
            return true;
        }
    }
    else if (type.getJavaType() == Double.class) {
        if (format == Value.FORMAT_FLOAT4) {
            buffer.writeLongLong(Double.doubleToLongBits(Float8.get(null, pValue)));
            return true;
        }
    }
    else if (type.getJavaType() == String.class) {
        if (format == Value.FORMAT_UTF8 && getEncoder() == Charsets.UTF_8) {
            buffer.writeLenStringUtf8(pValue);
            return true;
        }
    }
    return false;
}
 
Example #25
Source File: QCodec.java    From text_converter with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Default constructor.
 */
public QCodec() {
    this(Charsets.UTF_8);
}
 
Example #26
Source File: PacketEncoder.java    From antsdb with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * 
 * Registers a slave at the master. Should be sent before requesting a binlog events with COM_BINLOG_DUMP.
 * 
 * <pre>
 * Bytes                        Name
 * -----                        ----
 *    1              [15] COM_REGISTER_SLAVE
 *    4              server-id
 *    1              slaves hostname length
 *    string[$len]   slaves hostname
 *    1              slaves user len
 *    string[$len]   slaves user
 *    1              slaves password len
 *    string[$len]   slaves password
 *    2              slaves mysql-port
 *    4              replication rank
 *    4              master-id
 * 
 * @see https://dev.mysql.com/doc/internals/en/com-register-slave.html
 * </pre>
 * 
 */
public void writeRegisterSlave(PacketWriter buffer, int serverId) {
    // code for COM_REGISTER_SLAVE is 0x15 
    buffer.writeByte((byte)0x15);
    buffer.writeUB4(serverId);
    // usually empty
    buffer.writeLenString("", Charsets.UTF_8);
    // usually empty
    buffer.writeLenString("", Charsets.UTF_8);
    // usually empty
    buffer.writeLenString("", Charsets.UTF_8);
    // usually empty
    buffer.writeUB2(0);
    // replication rank to be ignored
    buffer.writeBytes(new byte[4]);
    // master id, usually 0
    buffer.writeUB4(0);
}
 
Example #27
Source File: CrossEncryptionTest.java    From oxAuth with MIT License 4 votes vote down vote up
private String encryptWithNimbusJoseJwt() {

        try {
            RSAKey senderJWK = (RSAKey) JWK.parse(senderJwkJson);

            RSAKey recipientPublicJWK = (RSAKey) (JWK.parse(recipientJwkJson));

            // Create JWT
//			SignedJWT signedJWT = new SignedJWT(
//			    new JWSHeader.Builder(JWSAlgorithm.RS256).keyID(senderJWK.getKeyID()).build(),
//			    new JWTClaimsSet.Builder()
//			        .subject("testi")
//			        .issuer("https:devgluu.saminet.local")
//			        .build());

            // Sign the JWT
            //		signedJWT.sign(new RSASSASigner(senderJWK));

            // Create JWE object with signed JWT as payload
            //		JWEObject jweObject = new JWEObject(
            //		    new JWEHeader.Builder(JWEAlgorithm.RSA_OAEP, EncryptionMethod.A128GCM)
            //		        .contentType("JWT") // required to indicate nested JWT
            //		        .build(),
            //		    new Payload(signedJWT));

            @SuppressWarnings("deprecation")
            JWEObject jweObject = new JWEObject(
                    new JWEHeader.Builder(JWEAlgorithm.RSA_OAEP, EncryptionMethod.A128GCM)
                            .type(JOSEObjectType.JWT)
                            .keyID(senderJWK.getKeyID())
                            .build(),
                    new Payload(Base64Util.base64urlencode(PAYLOAD.getBytes(Charsets.UTF_8))));


            // Encrypt with the recipient's public key
            RSAEncrypter encrypter = new RSAEncrypter(recipientPublicJWK);
            jweObject.encrypt(encrypter);

            //		System.out.println("Header: " +  jweObject.getHeader());
            //		System.out.println("Encrypted Key: " + jweObject.getEncryptedKey());
            //		System.out.println("Cipher Text: " + jweObject.getCipherText());
            //		System.out.println("IV: " + jweObject.getIV());
            //		System.out.println("Auth Tag: " + jweObject.getAuthTag());


            // Serialise to JWE compact form
            return jweObject.serialize();
        } catch (Exception e) {
            System.out.println("Error encryption with Nimbus: " + e.getMessage());
            return null;
        }
    }
 
Example #28
Source File: StorageAccessHelper.java    From andOTP with MIT License 4 votes vote down vote up
public static boolean saveFile(Context context, Uri file, String data) {
    return saveFile(context, file, data.getBytes(Charsets.UTF_8));
}
 
Example #29
Source File: BCodec.java    From text_converter with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Default constructor.
 */
public BCodec() {
    this(Charsets.UTF_8);
}
 
Example #30
Source File: PacketBuffer.java    From The-5zig-Mod with GNU General Public License v3.0 4 votes vote down vote up
public static String readString(ByteBuf byteBuf) {
	int length = byteBuf.readInt();
	String string = byteBuf.toString(byteBuf.readerIndex(), length, Charsets.UTF_8);
	byteBuf.readerIndex(byteBuf.readerIndex() + length);
	return string;
}