cancel
Showing results for 
Search instead for 
Did you mean: 

How to inject SiteService in my custom class?

mbel
Star Contributor
Star Contributor

Hello,

I would like to use some of the methods in Alfresco's SiteService and respectively its implementation.

For example - I want to list all currently created Sites in Alfresco system in a Java code.

What exactly should I inject in my code and what bean should I include.....Its not exactly clear for me.

Thank you in advance.

1 ACCEPTED ANSWER

You also need to add the setter, ie.

public void setSiteService(SiteService siteService)  

   this.siteService = siteService

}

Regards,

Jan

View answer in original post

22 REPLIES 22

kaynezhang
World-Class Innovator
World-Class Innovator

If  you want to develop alfresc  extensions  you can refer to building and packaging extensions into AMPs using Apache Maven ,add   <property name="siteService" ref="SiteService"/> into your spring module context.

If you want to develop client application ,you can use apache commons httpclient to call alfresco site webscript api.

mbel
Star Contributor
Star Contributor

I have already generated AMP using maven and have folder 'context' with 2 xml service-context and webscript-context.

I want the SiteService as property in my java CustomWebScript class, so I have define the CustomWebScript class in webscript-context.xml

<bean id="webscript.custom.get"  class="xxx.demoamp.CustomWebScript" parent="webscript" ></bean>

If I include in this bean <property name="siteService" ref="SiteService"/>

<bean id="webscript.custom.get" class="xxx.demoamp.CustomWebScript"  parent="webscript" >

           <property name="siteService" ref="SiteService"/>

  </bean>

also included  private SiteService siteService; as field in my custom Java class.

I receive the following error...

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webscript.custom.get' defined in class path resource [alfresco/module/xxx/context/webscript-context.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException:

Invalid property 'siteService' of bean class [xxx.demoamp.CustomWebScript]: Bean property 'siteService' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

So maybe I don't understand how exactly it works or I miss something...

Thank you in advance for your assistance.

You also need to add the setter, ie.

public void setSiteService(SiteService siteService)  

   this.siteService = siteService

}

Regards,

Jan

mbel
Star Contributor
Star Contributor

After added the setting everything is working fine. Thanks!

Would it work with @Inject or @Autowired annotations above the field or ?

In general I can use the existing webscript -> /alfresco/service/api/sites using HttpConnection and get the json object.

However, I will need the server context(host,port) or something in order not to hard-coded it , could you please advise with that option as well ?

kaynezhang
World-Class Innovator
World-Class Innovator

You can try to use org.springframework.beans.factory.annotation.Autowired.

I'm not clear what you mean about the second question.

mbel
Star Contributor
Star Contributor

Well, My second questions is based on your 2nd suggestion ",you can use apache commons httpclient to call alfresco site webscript api."

For example:

URL loginUrl = new URL("http://localhost:8080/alfresco/service/api/sites");

HttpURLConnection connection = (HttpURLConnection) loginUrl.openConnection();

connection.setRequestMethod("GET");

connection.connect();

InputStream is = connection.getInputStream();

//... and get the json object...

dont want to hard code the http://localhost:8080/ (since it can be changed) I want to get the current server/url context or something...not sure if there is such thing at all.

Or there is some other internal option all webscripts to communicate each other?

I will be really glad if you can answer me to the above questions as I am new with Alfresco and trying to understand how it works.....

Thanks.

kaynezhang
World-Class Innovator
World-Class Innovator

You can configure the base part(http://localhost:8080/alfresco/) in a property file in your application,every time when you call webscript api,you can build the full url by concating base part and path part.

mbel
Star Contributor
Star Contributor

I just found that there is SysAdminParams service, which provide the protocol, host, port and alfresco context.

kaynezhang
World-Class Innovator
World-Class Innovator

You want to call webscript api in alfresco ??