Java Code Examples for io.netty.handler.codec.base64.Base64#decode()

The following examples show how to use io.netty.handler.codec.base64.Base64#decode() . 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: KeyCertLoader.java    From WeCross with Apache License 2.0 6 votes vote down vote up
ByteBuf readPrivateKey(InputStream in) throws KeyException {
    String content;
    try {
        content = readContent(in);
    } catch (IOException e) {
        throw new KeyException("failed to read key input stream", e);
    }

    Matcher m = KEY_PATTERN.matcher(content);
    if (!m.find()) {
        throw new KeyException(
                "could not find a PKCS #8 private key in input stream"
                        + " (see https://netty.io/wiki/sslcontextbuilder-and-private-key.html for more information)");
    }

    ByteBuf base64 = Unpooled.copiedBuffer(m.group(1), CharsetUtil.US_ASCII);
    ByteBuf der = Base64.decode(base64);
    base64.release();
    return der;
}
 
Example 2
Source File: Http2ServerUpgradeCodec.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
/**
 * Decodes the settings header and returns a {@link Http2Settings} object.
 */
private Http2Settings decodeSettingsHeader(ChannelHandlerContext ctx, CharSequence settingsHeader)
        throws Http2Exception {
    ByteBuf header = ByteBufUtil.encodeString(ctx.alloc(), CharBuffer.wrap(settingsHeader), CharsetUtil.UTF_8);
    try {
        // Decode the SETTINGS payload.
        ByteBuf payload = Base64.decode(header, URL_SAFE);

        // Create an HTTP/2 frame for the settings.
        ByteBuf frame = createSettingsFrame(ctx, payload);

        // Decode the SETTINGS frame and return the settings object.
        return decodeSettings(ctx, frame);
    } finally {
        header.release();
    }
}
 
Example 3
Source File: PemReader.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
static ByteBuf readPrivateKey(InputStream in) throws KeyException {
    String content;
    try {
        content = readContent(in);
    } catch (IOException e) {
        throw new KeyException("failed to read key input stream", e);
    }

    Matcher m = KEY_PATTERN.matcher(content);
    if (!m.find()) {
        throw new KeyException("could not find a PKCS #8 private key in input stream" +
                " (see http://netty.io/wiki/sslcontextbuilder-and-private-key.html for more information)");
    }

    ByteBuf base64 = Unpooled.copiedBuffer(m.group(1), CharsetUtil.US_ASCII);
    ByteBuf der = Base64.decode(base64);
    base64.release();
    return der;
}
 
Example 4
Source File: PemReader.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
static ByteBuf readPrivateKey(File file) throws KeyException {
    String content;
    try {
        content = readContent(file);
    } catch (IOException e) {
        throw new KeyException("failed to read a file: " + file, e);
    }

    Matcher m = KEY_PATTERN.matcher(content);
    if (!m.find()) {
        throw new KeyException("found no private key: " + file);
    }

    ByteBuf base64 = Unpooled.copiedBuffer(m.group(1), CharsetUtil.US_ASCII);
    ByteBuf der = Base64.decode(base64);
    base64.release();
    return der;
}
 
Example 5
Source File: PemReader.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
static ByteBuf[] readCertificates(File file) throws CertificateException {
    String content;
    try {
        content = readContent(file);
    } catch (IOException e) {
        throw new CertificateException("failed to read a file: " + file, e);
    }

    List<ByteBuf> certs = new ArrayList<ByteBuf>();
    Matcher m = CERT_PATTERN.matcher(content);
    int start = 0;
    for (;;) {
        if (!m.find(start)) {
            break;
        }

        ByteBuf base64 = Unpooled.copiedBuffer(m.group(1), CharsetUtil.US_ASCII);
        ByteBuf der = Base64.decode(base64);
        base64.release();
        certs.add(der);

        start = m.end();
    }

    if (certs.isEmpty()) {
        throw new CertificateException("found no certificates: " + file);
    }

    return certs.toArray(new ByteBuf[certs.size()]);
}
 
Example 6
Source File: NettyHttpUtil.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
public static FullHttpResponse theBase64Image1pxGif() {
	ByteBuf byteBuf = Base64.decode(Unpooled.copiedBuffer(BASE64GIF_BYTES));
	FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK , byteBuf);
	response.headers().set(CONTENT_TYPE, ContentTypePool.GIF);
	response.headers().set(CONTENT_LENGTH, byteBuf.readableBytes());
	response.headers().set(CONNECTION, HEADER_CONNECTION_CLOSE);
	return response;
}
 
Example 7
Source File: NettyHttpUtil.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
public static FullHttpResponse theBase64Image1pxGif() {
	ByteBuf byteBuf = Base64.decode(Unpooled.copiedBuffer(BASE64GIF_BYTES));
	FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK , byteBuf);
	response.headers().set(CONTENT_TYPE, ContentTypePool.GIF);
	response.headers().set(CONTENT_LENGTH, byteBuf.readableBytes());
	response.headers().set(CONNECTION, HEADER_CONNECTION_CLOSE);
	return response;
}
 
Example 8
Source File: NettyHttpUtil.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
public static FullHttpResponse theBase64Image1pxGif() {
	ByteBuf byteBuf = Base64.decode(Unpooled.copiedBuffer(BASE64GIF_BYTES));
	FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK , byteBuf);
	response.headers().set(CONTENT_TYPE, ContentTypePool.GIF);
	response.headers().set(CONTENT_LENGTH, byteBuf.readableBytes());
	response.headers().set(CONNECTION, HEADER_CONNECTION_CLOSE);
	return response;
}
 
Example 9
Source File: NettyHttpUtil.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
public static FullHttpResponse theBase64Image1pxGif() {
	ByteBuf byteBuf = Base64.decode(Unpooled.copiedBuffer(BASE64GIF_BYTES));
	FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK , byteBuf);
	response.headers().set(CONTENT_TYPE, ContentTypePool.GIF);
	response.headers().set(CONTENT_LENGTH, byteBuf.readableBytes());
	response.headers().set(CONNECTION, HEADER_CONNECTION_CLOSE);
	return response;
}
 
Example 10
Source File: NettyHttpUtil.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
public static FullHttpResponse theBase64Image1pxGif() {
	ByteBuf byteBuf = Base64.decode(Unpooled.copiedBuffer(BASE64GIF_BYTES));
	FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK , byteBuf);
	response.headers().set(CONTENT_TYPE, ContentTypePool.GIF);
	response.headers().set(CONTENT_LENGTH, byteBuf.readableBytes());
	response.headers().set(CONNECTION, HEADER_CONNECTION_CLOSE);
	return response;
}
 
Example 11
Source File: NettyHttpUtil.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
public static FullHttpResponse theBase64Image1pxGif() {
	ByteBuf byteBuf = Base64.decode(Unpooled.copiedBuffer(BASE64GIF_BYTES));
	FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK , byteBuf);
	response.headers().set(CONTENT_TYPE, ContentTypePool.GIF);
	response.headers().set(CONTENT_LENGTH, byteBuf.readableBytes());
	response.headers().set(CONNECTION, HEADER_CONNECTION_CLOSE);
	return response;
}
 
Example 12
Source File: NettyHttpUtil.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
public static FullHttpResponse theBase64Image1pxGif() {
	ByteBuf byteBuf = Base64.decode(Unpooled.copiedBuffer(BASE64GIF_BYTES));
	FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK , byteBuf);
	response.headers().set(CONTENT_TYPE, ContentTypePool.GIF);
	response.headers().set(CONTENT_LENGTH, byteBuf.readableBytes());
	response.headers().set(CONNECTION, HEADER_CONNECTION_CLOSE);
	return response;
}
 
Example 13
Source File: Base64Renderer.java    From The-5zig-Mod with GNU General Public License v3.0 5 votes vote down vote up
private void prepareImage() {
	if (base64String == null) {
		delete(resourceLocation);
		this.dynamicImage = null;
		return;
	}
	ByteBuf localByteBuf1 = Unpooled.copiedBuffer(base64String, Charsets.UTF_8);
	ByteBuf localByteBuf2 = null;
	BufferedImage localBufferedImage;
	try {
		localByteBuf2 = Base64.decode(localByteBuf1);
		localBufferedImage = read(new ByteBufInputStream(localByteBuf2));
		Validate.validState(localBufferedImage.getWidth() == width, "Must be " + width + " pixels wide");
		Validate.validState(localBufferedImage.getHeight() == height, "Must be " + height + " pixels high");
	} catch (Exception e) {
		The5zigMod.logger.error("Could not prepare base64 renderer image", e);
		delete(resourceLocation);
		dynamicImage = null;
		return;
	} finally {
		localByteBuf1.release();
		if (localByteBuf2 != null) {
			localByteBuf2.release();
		}
	}
	if (this.dynamicImage == null) {
		this.dynamicImage = The5zigMod.getVars().loadDynamicImage(resourceLocation.callGetResourcePath(), localBufferedImage);
	}
}
 
Example 14
Source File: PemReader.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
static ByteBuf[] readCertificates(InputStream in) throws CertificateException {
    String content;
    try {
        content = readContent(in);
    } catch (IOException e) {
        throw new CertificateException("failed to read certificate input stream", e);
    }

    List<ByteBuf> certs = new ArrayList<ByteBuf>();
    Matcher m = CERT_PATTERN.matcher(content);
    int start = 0;
    for (;;) {
        if (!m.find(start)) {
            break;
        }

        ByteBuf base64 = Unpooled.copiedBuffer(m.group(1), CharsetUtil.US_ASCII);
        ByteBuf der = Base64.decode(base64);
        base64.release();
        certs.add(der);

        start = m.end();
    }

    if (certs.isEmpty()) {
        throw new CertificateException("found no certificates in input stream");
    }

    return certs.toArray(new ByteBuf[certs.size()]);
}
 
Example 15
Source File: HttpProxyServer.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private boolean authenticate(ChannelHandlerContext ctx, FullHttpRequest req) {
    assertThat(req.method(), is(HttpMethod.CONNECT));

    if (testMode != TestMode.INTERMEDIARY) {
        ctx.pipeline().addBefore(ctx.name(), "lineDecoder", new LineBasedFrameDecoder(64, false, true));
    }

    ctx.pipeline().remove(HttpObjectAggregator.class);
    ctx.pipeline().get(HttpServerCodec.class).removeInboundHandler();

    boolean authzSuccess = false;
    if (username != null) {
        CharSequence authz = req.headers().get(HttpHeaderNames.PROXY_AUTHORIZATION);
        if (authz != null) {
            String[] authzParts = authz.toString().split(" ", 2);
            ByteBuf authzBuf64 = Unpooled.copiedBuffer(authzParts[1], CharsetUtil.US_ASCII);
            ByteBuf authzBuf = Base64.decode(authzBuf64);

            String expectedAuthz = username + ':' + password;
            authzSuccess = "Basic".equals(authzParts[0]) &&
                           expectedAuthz.equals(authzBuf.toString(CharsetUtil.US_ASCII));

            authzBuf64.release();
            authzBuf.release();
        }
    } else {
        authzSuccess = true;
    }

    return authzSuccess;
}
 
Example 16
Source File: KeyCertLoader.java    From WeCross with Apache License 2.0 5 votes vote down vote up
public ByteBuf[] readCertificates(InputStream in) throws CertificateException {
    String content;
    try {
        content = readContent(in);
    } catch (IOException e) {
        throw new CertificateException("failed to read certificate input stream", e);
    }

    List<ByteBuf> certs = new ArrayList<ByteBuf>();
    Matcher m = CERT_PATTERN.matcher(content);
    int start = 0;
    for (; ; ) {
        if (!m.find(start)) {
            break;
        }

        ByteBuf base64 = Unpooled.copiedBuffer(m.group(1), CharsetUtil.US_ASCII);
        ByteBuf der = Base64.decode(base64);
        base64.release();
        certs.add(der);

        start = m.end();
    }

    if (certs.isEmpty()) {
        throw new CertificateException("found no certificates in input stream");
    }

    return certs.toArray(new ByteBuf[0]);
}
 
Example 17
Source File: Base64Renderer.java    From The-5zig-Mod with MIT License 5 votes vote down vote up
private void prepareImage() {
	if (base64String == null) {
		delete(resourceLocation);
		this.dynamicImage = null;
		return;
	}
	ByteBuf localByteBuf1 = Unpooled.copiedBuffer(base64String, Charsets.UTF_8);
	ByteBuf localByteBuf2 = null;
	BufferedImage localBufferedImage;
	try {
		localByteBuf2 = Base64.decode(localByteBuf1);
		localBufferedImage = read(new ByteBufInputStream(localByteBuf2));
		Validate.validState(localBufferedImage.getWidth() == width, "Must be " + width + " pixels wide");
		Validate.validState(localBufferedImage.getHeight() == height, "Must be " + height + " pixels high");
	} catch (Exception e) {
		The5zigMod.logger.error("Could not prepare base64 renderer image", e);
		delete(resourceLocation);
		dynamicImage = null;
		return;
	} finally {
		localByteBuf1.release();
		if (localByteBuf2 != null) {
			localByteBuf2.release();
		}
	}
	if (this.dynamicImage == null) {
		this.dynamicImage = The5zigMod.getVars().loadDynamicImage(resourceLocation.getResourcePath(), localBufferedImage);
	}
}