Skip to content

Instantly share code, notes, and snippets.

View ldclakmal's full-sized avatar
🎯
Focusing

Chanaka Lakmal ldclakmal

🎯
Focusing
View GitHub Profile
@ldclakmal
ldclakmal / Stemmer.java
Created January 21, 2018 02:47
Stemmer, implementing the Porter Stemming Algorithm
package DM;
/*
Porter stemmer in Java. The original paper is in
Porter, 1980, An algorithm for suffix stripping, Program, Vol. 14,
no. 3, pp 130-137,
See also http://www.tartarus.org/~martin/PorterStemmer
@ldclakmal
ldclakmal / MSF4J.java
Last active July 13, 2018 08:45
Microservice runner with netty-transports.yml
package org.wso2.cse.rest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wso2.cse.configuration.AuthInterceptor;
import org.wso2.cse.service.Constant;
import org.wso2.msf4j.MicroservicesRunner;
import java.io.File;
import java.io.IOException;
@ldclakmal
ldclakmal / RESTAPI.java
Last active April 22, 2018 18:58
Call REST API
private Optional<JsonObject> callRestApi(String url) {
try {
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials =
new UsernamePasswordCredentials(System.getenv(Constant.EnvironmentVariable.USERNAME),
System.getenv(Constant.EnvironmentVariable.PASSWORD));
provider.setCredentials(AuthScope.ANY, credentials);
HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
HttpResponse response = client.execute(new HttpGet(url));
String json = EntityUtils.toString(response.getEntity(), "UTF-8");
@ldclakmal
ldclakmal / basic-auth-0.980.0.bal
Last active January 17, 2020 10:42
Ballerina basic authentication
endpoint http:Client basicAuthClient {
url: "https://www.example.org/api/v1",
auth: {
scheme: http:BASIC_AUTH,
username: "admin",
password: "123"
}
};
public function testBasicAuth() {
@ldclakmal
ldclakmal / bearer-auth-0.980.0.bal
Last active January 17, 2020 10:41
Ballerina bearer token authentication
endpoint http:Client bearerTokenAuthClient {
url: "https://www.example.org/api/v1",
auth: {
scheme: http:OAUTH2,
accessToken: "yf29.PlrfBb0gtDFXsbnE_LcDCG-Dz3djEp05zM9y-IPR8CsZz90XwOEyrhqeXPPYxubY9RHMIFzoV2"
}
};
public function testBearerTokenAuth() {
string requestPath = "/employees";
@ldclakmal
ldclakmal / oauth2-0.980.0.bal
Last active January 17, 2020 10:42
Ballerina OAuth2 authentication
endpoint http:Client oauthClient {
url: "https://www.example.org/api/v1",
auth: {
scheme: http:OAUTH2,
accessToken: "yf29.PlrfBb0gtDFXsbnE_LcDCG-Dz3djEp05zM9y-IPR8CsZz90XwOEyrhqeXPPYxubY9RHMIFzoV2",
clientId: "50332352747-270vchcnbhbl4gfn1v91hl0fru.apps.example.org",
clientSecret: "PrfEadE4s2SDG4hJ",
refreshToken: "5/lk-u0Ywefgh52-v0_OLOkDhlsA9xIadf4qqD3TMQvc",
refreshUrl: "https://www.example.com/oauth2/v3/token"
}
@ldclakmal
ldclakmal / jwt-auth-0.980.0.bal
Last active January 17, 2020 10:40
Ballerina JWT authentication
endpoint http:Client jwtAuthClient {
url: "https://www.example.org/api/v1",
auth: {
scheme: http:JWT_AUTH
}
};
public function testJWTAuth() {
setJwtTokenToAuthContext();
string requestPath = "/employees";
@ldclakmal
ldclakmal / auth.bal
Created June 23, 2018 12:03
Ballerina authentication sample
import ballerina/io;
import ballerina/http;
import ballerina/config;
import ballerina/runtime;
function main(string... args) {
testNoAuth();
testBasicAuth();
testOAuth2();
testJWTAuth();
@ldclakmal
ldclakmal / http2-client-1.1.0.bal
Last active January 17, 2020 09:54
Ballerina HTTP/2 client
import ballerina/http;
import ballerina/log;
http:Client clientEP = new("http://localhost:9095", config = { httpVersion: "2.0" });
public function main() {
var responseVar = clientEP->post("/hello/sayHello", "Hello Ballerina!");
if (responseVar is http:Response) {
var payload = responseVar.getTextPayload();
if (payload is string) {
@ldclakmal
ldclakmal / http2-server-1.1.0.bal
Last active January 17, 2020 09:54
Ballerina HTTP/2 service
import ballerina/http;
listener http:Listener helloWorldEP = new(9095, { httpVersion: "2.0" });
service hello on helloWorldEP {
resource function sayHello(http:Caller caller, http:Request req) {
checkpanic caller->respond("Ballerina HTTP/2 Response !");
}
}