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
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
No comments:
Post a Comment