Wednesday 3 August 2016

Adding file to Documents and Media(dlfileentry table) from ServiceImpl class liferay through web service

In this post I will upload file to dlfileentry table through my EntityServiceImpl class.

My intention is to add/upload a file to dcouments and media through my web service.

First we created one entity in service.xml file

<entity name="MyServiceGateway" local-service="true" remote-service="true">
</entity>

This will use as gateway for web service and not creating any column inside it.
I want to use only its services as generated after service build.

remote service must be true for creating web service.

For web service you have to do some configuration.
Please go through this post:
http://liferayasif.blogspot.my/2016/06/expose-liferay-web-services.html

After all above configuration add this code to in MyServiceGatewayServiceImpl class.
Note:
    If you change method signature even return type then we have to build service again
When you modified some code inside method and no modification in method signature then only portlet deployment requires no need of build service.
Please also do build wsdd.

@AccessControlled(guestAccessEnabled=true)
public void fileUpload(File file) throws SystemException, PortalException{

if(Validator.isNotNull(file)){
try{
long companyId = CompanyThreadLocal.getCompanyId();
String groupName = GroupConstants.GUEST;
Group group = GroupLocalServiceUtil.getGroup(companyId, groupName);
long groupId = group.getGroupId();
long repositoryId = groupId;
ServiceContext serviceContext = new ServiceContext();
serviceContext.setAddCommunityPermissions(true);
serviceContext.setAddGroupPermissions(true);
serviceContext.setAddGuestPermissions(true);
serviceContext.setCompanyId(companyId);
serviceContext.setScopeGroupId(groupId);
serviceContext.setDeriveDefaultPermissions(true);
serviceContext.setDeriveDefaultPermissions(true);
InputStream is = new FileInputStream( file );
String mimeType = MimeTypesUtil.getContentType(file);
String description = "added programmatically";
                                String title = file.getName();
try{
FileEntry fileEntry = DLAppServiceUtil.addFileEntry(repositoryId, 0, file.getName(), mimeType, title, description, "", is, file.getTotalSpace(), serviceContext);
log.info("fileEntry :"+fileEntry);
}
catch(Exception e2){
e2.printStackTrace();
}
log.info("absolute path: "+file.getAbsolutePath());
}
catch(Exception e){
log.info("outer group err: "+e.getMessage());
}
}
else{
log.info("File is null");
}
}

URL of our web service will get from
first open
localhost:8080/api/jsonws
Then select your plugin project from context path drop down.
Select specific method from list
If you press invoke and then go to url example you can see url of your web service.

Now if we hit it postman then it gives error that authentication requires.
So you have to hit by using credentials.
May be this is not secure but may be helpful for someone.

Now how to hit from postman.
I am pasting some snap shot of postman
First go Authorisation tab, from type drop down select Basic Auth.
Give username and password then update request.

After this step check this snap shot
In headers Authorisation value will generate automatically.




In body tab select form data radio button add key value.
On the basic of parameter please add this key value.
Key must mtch with your arguments name.
Suppost I have single formal argument for method
e.g.
myMethod(File myFile){
    //Here code
}
Then key in Body tab must be myFile, match with our formal arguments.

And then finally this snap shot



Then hit.

If you face any problem please comment, if possible I will try to reply.
Thanks

Monday 1 August 2016

liferay web service file upload

Hi
In this post I will first create web service and with the help of post man I will upload a file.
Here I will also increase the size of column using "portlet-model-hints.xml" file.
First I say thanks to Sudheer Kumar liferayway helps me to get the concept of increase of size of column.

We use liferay build service to create table
I am using two tables one is a just gateway to access other services.
My service.xml is

<entity name="EDServiceGateway" local-service="true" remote-service="true">
</entity>

<entity name="FileUploader" local-service="true">
  <column name="fid" type="long" primary="true"/>
    <column name="content" type="String"/>
</entity>

Then build service and also build WSDD.

After build service open /WEB-INF/sql/tables.sql

You can see
create table fileuploader (
fid LONG not null primary key,
content VARCHAR(75) null
);
Here content is name of our column and the default size is VARCHAR(75) if we use String data type in our service.xml file.
To increase its size we have to follow these steps

Open portlet-model-hints.xml

<model name="com.slayer.model.FileUploader">
<field name="fid" type="long" />
<field name="content" type="String" />
</model>

Above is default generated sql by default after service build.
We want to increase the size of content column.
To do this we have to modify file as below code.

<model name="com.slayer.model.FileUploader">
<field name="fid" type="long" />
<field name="content" type="String">
<hint-collection name="CLOB" />
</field>
</model>

This is our final code.
If you have source code of liferay portal then you can search a file.
portal-model-hints.xml.
In this file hint-collection="CLOB" already declared with very big size.
After above changes then build service again.

Now this time check your tables.sql file

create table fileuploader (
fid LONG not null primary key,
content TEXT null
);
Size of content column changes to TEXT type.

Please don't forget to build WSDD.

Number of services will create.

Here I am using FileUploaderLocalServiceImpl to upload file to db.

import java.io.File;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.Base64;
import com.liferay.portal.kernel.util.FileUtil;

public class FileUploaderLocalServiceImpl
extends FileUploaderLocalServiceBaseImpl {

Log log = LogFactoryUtil.getLog(FileUploaderLocalServiceImpl.class);

public boolean add(File file){
   log.info("add()");
byte[] bt;
    long fid=0L;
    try {
     fid=CounterLocalServiceUtil.increment();
     FileUploader f = fileUploaderLocalService.createFileUploader(fid);
     bt = FileUtil.getBytes(file);
     f.setContent(Base64.objectToString(bt));
     fileUploaderPersistence.update(f, false);
    } catch (Exception e) {
    log.info("Err: "+e.getMessage());
    return false;
    }
    return true;
}

}


Class that expose web services
public class EDServiceGatewayServiceImpl extends
EDServiceGatewayServiceBaseImpl {

Log log = LogFactoryUtil.getLog(EDServiceGatewayServiceImpl.class);

  @AccessControlled(guestAccessEnabled=true)
public JSONArray fileUpload(File file) throws SystemException, PortalException{

  System.out.println("---------------->"+file);
  System.out.println("---------------->"+file.getAbsolutePath());
  log.info("file : "+file);
 File f = file;
 boolean bool = fileUploaderLocalService.add(file);
 log.info("success: "+bool);
  log.info("file name : "+f.getName());
 JSONArray ar = JSONFactoryUtil.createJSONArray();

 return ar;
}

}


if you try this one from
your ip:8080/api/jsonws/EDModule-portlet.edgateway/file-upload
Then it gives one error



This is I don't know why but you can access it through post man

First copy web service url



From here you get the url of generated web service.
Use this web url from post man.


Hit send and hope so it will work.
Thanks