Tuesday, August 6, 2019

Rest apis powered by GraphQL

We can understand "Rest Api" means any system expose its functionality over http. So the consumer of api can make a http request to avail the functionalities. JSON is the data format in which client and server communicate over http.

GraphQL is a query language for api which enable the client of rest apis to ask what they need exactly. The same time server side its super easy to expose more functionaries in a generic way and cater lots of specialized request. 

more details # https://graphql.org/

GarphQL in Java # https://www.graphql-java.com/

In this article we will see an example where GraphQL is used with JAX-RS (Jersey) based rest implementation. And we will use Hibernate as Data Fetcher for GraphQL.

Its a standard gradle project, created with eclipse.  Here is the link to my github.

https://github.com/pallabrath/myexpjava/tree/master/GqlDemo

1. Lets start with build.gradle

We have the dependencies for jersey, graphql, hibernate, ojdbc driver and some helpers like gson and gauva.

2. In web.xml we have initialised the jersey servlet, there we have specified init parameters to look for com.rest package.

3. Inside source we have com.rest.Employee.java which expose the rest end point GqlDemo/rest/employee

We have the Query() method to handle the incoming http post request for graph ql queries.

4. We have created GraphQl provider which load the schema definitions from resources/schema.graphqls
we have defined the wiring for query employeeById.

5. In GraphQLDataFetcher we have the implementation for employeeById. where we get the request parameter value and process. Here I have used hibernate to query the database.

Here is a screen shot of postman request/response.

I will try to answer if any question on this. Please let me know your feedback.



Saturday, February 9, 2019

Read ZipEntry form ZipInputStream as InputStream

To read ZipEntry from ZipInputStream

public class ZipEntityDemo {
public InputStream extract(ZipInputStream zis, String entityName) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ZipEntry ze = null;
do {
ze = zis.getNextEntry();
if (ze != null && ze.getName().equalsIgnoreCase(entityName)) {
byte[] buffer = new byte[1];
while (zis.available() == 1) {
zis.read(buffer);
out.write(buffer, 0, 1);
}
out.close();
return new ByteArrayInputStream(out.toByteArray());
}
} while (ze != null);
return null;
}
}
Here zis.getNextEntry() will return us the beginning of an entry. If the name of the entry match to our desired name we can start reading. Here we are reading byte by byte in a loop till last byte. We could have improved by using ze.getSize(), and we could read multiple bytes till the size. But the getSize() methods some time returns  -1 for some zipfiles. So it wouldn't be reliable. With out knowing the entry size only way is to read byte by byte till last.

Sunday, January 13, 2019

How to Unzip in Java

Here is a quick java program to unzip or explode a zip file. We will read the zip file with the help of java.util.ZipFile, then we will process each entries inside this. We will read the entries as stream and write to a buffer and eventually to a file output stream.  So at the end we will extract all the elements in the zip, and maintain the same hierarchical structure of directories .Please refer the below example.

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class UnZipDemo {
public static void main(String args[]) throws IOException
{
String zipFileName = "D:\\demo.zip";
ZipFile zipFile = new ZipFile(zipFileName);
String extractedPath = "D:\\demo";
byte[] buffer = new byte[1024];
Enumeration enm = zipFile.entries();
ZipEntry ze = null;
InputStream fis = null;
while (enm.hasMoreElements()) {
ze = (ZipEntry) enm.nextElement();
System.out.println("processing = "+ze.getName());
File newFile = new File(extractedPath + File.separator + ze.getName());
// create all non exists folders
new File(newFile.getParent()).mkdirs();
FileOutputStream fos = new FileOutputStream(newFile);
int len;
fis = zipFile.getInputStream(ze);
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
fis.close();
}
}
}