Custom message on redirect page when Role based access denied using spring security in Hybris | use of spring security in Hybris

Hello Friends ,
In our project there was requirement that when user has no access to any page then login page should come with custom message like "You have no permission to see page without login So please do login "
We all ready know in Hybris there is out of box spring security which is used to configure to redirect page if visitor has no access to see page but how the your custom message will display on login page for that you need to customize in Hybris.
 
First you should know how out of box redirect is working
open file
hybris\bin\ext-template\yacceleratorstorefront\web\webroot\WEB-INF\config\spring-security-config.xml

See code

<security:http disable-url-rewriting="true" pattern="/checkout/**" use-expressions="true">
        <security:anonymous username="anonymous" granted-authority="ROLE_CUSTOMERGROUPS" />
                                <security:access-denied-handler error-page="/login"/>
</security:http>


so within this page if for URL pattern if user has no role which is define like granted-authority="ROLE_CUSTOMERGROUPS"  then it will redirect to login page by using code

 
<security:access-denied-handler error-page="/login"/>



Now question is
 how to display custom message or any custom logic you want to execute before call your login then you need to use one of spring security property which is override

1. first create a custom class MyAccessDeniedHandler by implementing AccessDeniedHandler (spring security class)

 
public class MyAccessDeniedHandler  implements AccessDeniedHandler
{
}

2. Now override method
 
@Override
                public void handle(HttpServletRequest request, HttpServletResponse response,
                                AccessDeniedException accessDeniedException)
                throws IOException, ServletException {

                                //do some business logic, then redirect to errorPage url
                                response.sendRedirect(errorPage);

                }
              

             
3. After declare your bean in in your security file as

 
<bean id="myAccessDeniedHandler" class="com.yaccelrator.storefront.security.impl.MyAccessDeniedHandler">
</bean>
4.  Replace security handler with below

 
<security:http disable-url-rewriting="true" pattern="/checkout/**" use-expressions="true">
        <security:anonymous username="anonymous" granted-authority="ROLE_CUSTOMERGROUPS" />
                                <security:access-denied-handler ref="myAccessDeniedHandler"/>
</security:http>



Now you can
 customize to handler url

Note:  Spring security class
 AccessDeniedHandler provide to perform some business logics before redirect


How to use DCEVM a alternative of JRebel with Hybris ? Step by step to configure DCEVM with Hybris

                                                         DCEVM + Hybris
Those of you who work with hybris development on a daily basis know very well that application build and server restarting is annoying. when you make just a small change in your Java class, it still takes at least a 3-5 minutes for the change to be reflected because the whole application will need to be build and restart server.
Dynamic Code Evolution Virtual Machine (DCEVM) is fast tracks Java application development by skipping the time consuming build and server restart steps in the development process. DCEVM makes developers more productive since they can view code changes in real time.
DCEVM can use in two mode with hybris. You can use any one of below for your Hybris project
1.  Only using DCEVM patch. -No require any configuration in hybris's Local.properties file
2.  DCEVM patch + Hotsawp agent. - require configuration in hybris's Local.properties file.

DCEVM patch
Below steps you need to require to run your hybris project  with DCEVM patch
1.  Before download check your java version installed in your system ( JDK 8 or JDK 7) and set
     JAVA_HOME
    

2. Now download DCEVM patch ( version according to your system's JDK)
    https://github.com/dcevm/dcevm/releases

Now run DCEVM installer and install "Install DCEJVM as altjvm" as below


3.  Now start your eclipse and do your eclipse workspace setting
     windows > preferences > workspace  check as below


Now import your Hybris project in workspace
After import your project,  make sure that your eclipse's build automatically is enable  and it should successfully  build the hybris project.

So for successfully eclipse build you need to import all extension in your workspace which is require to make the build successfully.
After import all your require extension, your workspace looks lot of extension but your's project specific you need only few extension only then you can use one of eclipse features "Working Set"
4. Now start your hybris server in debug mode
   > hybrisserverstsrt.bat debug
   After that start your eclipse in debug mode
 Now you can do change in your java file and save (because of your eclipse "build automatically" is  enable  so it will start your project build automatically just after save) then you can check you .class file changes.
Note: -  So DCEVM patch only work in DEBUG mode so always you need to make sure your server and eclipse is running in debug mode.
                                                   
                                            DCEVM patch + Hotswap
Below steps you need to require to run your hybris project  with DCEVM patch + hotswap
1.  Apart from above setting means install correct DCEVM installer + JAVA_HOME setting  , You need  download  hotswap agent
https://github.com/HotswapProjects/HotswapAgent/releases

2.  In this mode there is no require your eclipse's "build automatically " enable  so you disable these feature form your eclipse workspace as below

only Refresh will check box will checked because of in this mode build will do from outside of your eclipse so your eclipse workspace should refresh if any file changing from outside .

3. Now start your eclipse and import your hybris project into workspace . In this mode you can import your's require extension only because in this mode project will build form outside , Not from your eclipse's "build automatically" features.
4. Now you need to configure your Hotswap agent in your Local.properties file as below
tomcat.debugjavaoptions=-XXaltjvm=dcevm -javaagent:E:/hotswapagent/hotswap-agent-1.1.0-SNAPSHOT.jar=autoHotswap=true,disablePlugin=hibernate,disablePlugin=jetty,disablePlugin=JSF,disablePlugin=ZK,disablePlugin=Weld,disablePlugin=Seam -Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,server=y,address=8000,suspend=n
in above you refer location of Hotsawp Jar file as above
5. Now start your hybris server in debug mode
   > hybrisserverstart.bat debug

you can check your hybris log to start hybris server with hotswap

6. After start your hybris server , You can make change your in java file then you need to build your project by opening new command prompt and do build as below
   > ant build

 then after you can see your change of .class file
after ant build , If hotswap do successfully that can check your server log as below
Note :-  In second mode it is not mandatory to run your eclipse in debug mode 






Why and what is use of typecode in Items in Hybris? | explain use of typecode in hybris?


Hello Friends,

Today I want to discuss some useful topic with you about related to typecode in hybris 

Have you ever think of below code

modelService.get(PK) -it will gives result of model of that primary key 

So from above code it seems that in hybris, Primary Key (PK) of all items of whole types is unique 

So again my question is that when above code is execute.  Is hybris search all items in all table in hybris?

Surely answer is NO then how model Service search that items ?

Concept - So here typecode of items comes in picture. Basically in hybris "have you ever think how PK generate in hybris". Primary Key of item of type is generate from using of below things
  a. typeCode of type
  b. getClusterID();
  c. CreationTime();
 
So by above combination of logic , primary key of item created in hybris 

So when you write modelService.get (PK) then from PK it extract typecode of ITEM then it directly apply search in particular table. So I think you got understand about typecode

Explain core topic extension, extension module, extension template in Hybris? | Hybris interview question for fresher


Hello Friends,
Today I choose very basic topic of hybris and it is extension, I know that you all are aware about extension but let’s read again.

What is extension?
Basically extension is a unit of functionality in hybris. In other work you can say each extension is used for a specific functionality in hybris. If you look the architecture of Hybris then you can found that if you need to customize any functionality of Hybris then you do by creating your own extension so every extension in hybris contains
1. Business logic
2. Type definition (items.xml)
3. Web application
4. hmc configuration
Next question is What is extension module ?
In my last Hybris interview I got question that what is extension and extension module. So basically extension module is building block or you can say structural element of extension
lets take example of yacceleratorstorefront, where if you look in their extensioninfo.xml file then you found
 

<webmodule jspcompile="false" webroot="/yacceleratorstorefront"/>
 

Let’s take an example of yacceleratorcore, where if you look in their extenioninfo.xml file then you found
 

<coremodule generated="true" manager="de.hybris.platform.yacceleratorcore.jalo.YAcceleratorCoreManager" packageroot="de.hybris.platform.yacceleratorcore"/>
<hmcmodule extensionclassname="de.hybris.platform.yacceleratorcore.hmc.YAcceleratorCoreHMCExtension"/>
 

so all above webmodule, coremodule, hmcmodule is called extension module and each module has offers a certain set of functionality
below details of functionality of each module given above
Coremodule -Declarations and localizations of types, Jalo item classes (items.xml)
Webmodule - Web application
Hmcmodule - hybris Management Console (hMC) configuration and localization for it, custom hybris Management Console elements, such as editors, tabs, display chips.
 

Now my final question is what is extension template?

I think you already know about extgen - is ant command to create extension. Basically for new extension you are choose extension template like yempty etc so that yempty is extension template.

Explain Core concept of autocreate and generate in hybris items | items concept in hybris?


Clear Concept about autocreate and generate in Hybris item
In many time hybris developer become confuse or not able to give clear answer related autocreate and generate in hybris item.

So today I have decided to go with autocreate and generate
I know every hybris developer who started reading this post they will think that I know but let’s check your concept with my scenario

1. What meaning of autocreate and generate
Answer: as per hybris wiki autocreate means make database entry during initialization and update for this item and generate is responsible to create generated java class and jalo class
2. So my second question is what is meaning of database entry for autocreate
   

   suppose your item is             

  <itemtype code="EveryreplyCustTest2" generate="false" autocreate="false">
                   <description>It represents everyreply Customer.</description>
                   <attributes>
                       <attribute qualifier="customerId" type="java.lang.String" >
                    <persistence type="property" />
                   <description>It is localized customerId of the everyreply user.</description>
                 </attribute>
                  </attributes>
    </itemtype>   

If you give autocreate="false" then it will give Build error means you are restricting to create model class then there is no any meaning of this item in hybris because defining items means data model then
Why it autocreate given in Hybris while must have to create model class?
Because sometime you extending Items then at time you are doing something on existing model class then there is not require to create model class again so that’s why Hybris given attribute autocreate on Item definition level


Now again autocreate comes under to define attribute level also as below            

<attributes>
<attribute qualifier="customerId" type="java.lang.String" autocreate="true">
<persistence type="property" />
<description>It is localized customerId of the everyreply user.</description>
 </attribute>

 In above autocreate follow same rule as Item level definition but for this it will follow rule inside your Model class if it is false then this attribute will missing in your model class
3. Now comes to use Generate in Items level and Attribute definition level
Generate is responsible to create jalo class and generated class in extension. If you give generate="false" then after build there will create jalo class and generated class in your extension.
 
 


<itemtype code="EveryreplyUserTest1"  autocreate="true" generate="false" >
<description>Apparel size variant type that contains additional attribute describing variant size.</description>
     <attributes>
          <attribute qualifier="userid" type="java.lang.String">
          <description>Size of the product.</description>
                        <modifiers initial="true" write="false"/>
                        <persistence type="property"/>
                    </attribute>
                </attributes>
            </itemtype>

If you give generate="false" and given jaloclass as below



<itemtype code="EveryreplyUserTest1"  autocreate="true" generate="false" jaloclass="de.hybris.platform.yacceleratorcore.jalo.EveryreplyUserTest1" >
                <description>Apparel size variant type that contains additional attribute describing variant size.</description>
                <attributes>
                <attribute qualifier="userid" type="java.lang.String">
                        <description>Size of the product.</description>
                        <modifiers initial="true" write="false"/>
                        <persistence type="property"/>
                    </attribute>
                </attributes>
            </itemtype>
 

 Then it give build error
In attribute level Generate="true" is responsible for creating setter and getter inside the model class. As you there another modifier used in attribute
   <modifiers write="true" read="true"/>


It is responsible for creating setter and getter inside the model class then definitely in your mind question is coming that why used generate while read and write is responsible for setter and getter so below combination will make you ok

   A. generate="false" and in modifier read="true" write="true" > build error
   B. generate="true" and in modifier read="false" write="true" > only setter will create
   C. generate="true" and in modifier read="true" write="false" > only getter is      create

 

How to write a Abortable Cron Job in Hybris | explain how to make Abortable Cron Job

In my latest Hybris Interview, I got question related Abortable Cron Job means How to Abort a Cron Job?
Let’s first understand it, first assume that from last few hours your Cron Job is running and now i want to Abort this Cron Job then how can i do it.
Concept: You can only abort those Cron Job which satisfies below two conditions 
A. During creation of your cron job override method isAbortable() and make return      it TRUE means now you can Abort this Cron job.
B. Second through Configurable in your Spring.xml file as below
   <property name="abortable" value="true"/>
After above those you can abort your cron job means your Cron Job is Abortable 
Also you can abort by below code
cronJobService.requestAbortCronJob(running_cronjob_code);

A abortable cron job's result as ERROR and status as ABORTED

Explain Solr boost in Hybris | How can add more index property in free text search in Hybris ?

Sometime in Hybris interview we get question which I heard first time so obviously unable to give answer of that question

Yesterday i got question about solr boost So first we need understand what is Solr boost
> Suppose you using
 free text search on storefront in hybris means doing search something text like "123" then at hybris side solr query fire on solr server and display result on screen what he found in result so result will definitely happen on some index property of your Solr item type's index property like ean,code,name etc
Let's suppose of text "123" match in all these 3 index property (ean,code,name) and you want display product (Ean match first) top on result so these type of result we can achieve through solr boost where we boost the search result by providing high score (boost) to index property.

Before going to
 solr boost first another question is How can we add more index property in free text search in hybris

Technical

in
 hybris there one file name commerceservices-spring-solrfacetsearch.xml(hybris 5.5) where you can found defaultCommerceSearchTextPopulator where we need to add your index property to apply free text search
suppose there is index property (description) on which you want to apply free text search then you need to add this property as below

<alias name="defaultCommerceSearchTextPopulator" alias="commerceSearchTextPopulator"/>
<bean id="defaultCommerceSearchTextPopulator" class="de.hybris.platform.commerceservices.search.solrfacetsearch.populators.SearchTextPopulator">
        <property name="freeTextQueryBuilders">
            <list>
 <bean class="de.hybris.platform.commerceservices.search.solrfacetsearch.querybuilder.impl.DefaultFreeTextQueryBuilder">
                    <property name="propertyName" value="ean"/>
                    <property name="boost" value="100"/>
                </bean>
<bean class="de.hybris.platform.commerceservices.search.solrfacetsearch.querybuilder.impl.DefaultFreeTextQueryBuilder">
                    <property name="propertyName" value="code"/>
                    <property name="boost" value="90"/>
                </bean>
 <bean class="de.hybris.platform.commerceservices.search.solrfacetsearch.querybuilder.impl.DefaultFreeTextQueryBuilder">
                    <property name="propertyName" value="name"/>
                    <property name="boost" value="50"/>
                </bean>
                <bean class="de.hybris.platform.commerceservices.search.solrfacetsearch.querybuilder.impl.DefaultFreeTextQueryBuilder">
                    <property name="propertyName" value="manufacturerName"/>
                    <property name="boost" value="40"/>
                </bean>
<bean class="de.hybris.platform.commerceservices.search.solrfacetsearch.querybuilder.impl.ClassificationFreeTextQueryBuilder">
                    <property name="boost" value="30"/>
                </bean>
                <bean class="de.hybris.platform.commerceservices.search.solrfacetsearch.querybuilder.impl.DefaultFreeTextQueryBuilder">
                    <property name="propertyName" value="keywords"/>
                    <property name="boost" value="20"/>
                </bean>
<bean class="de.hybris.platform.commerceservices.search.solrfacetsearch.querybuilder.impl.DefaultFreeTextQueryBuilder">
                    <property name="propertyName" value="categoryName"/>
                    <property name="boost" value="10"/>
                </bean>
<bean class="de.hybris.platform.commerceservices.search.solrfacetsearch.querybuilder.impl.DefaultFreeTextQueryBuilder">
                    <property name="propertyName" value="description"/>
                    <property name="boost" value="10"/>
                </bean>
               
            </list>
        </property>
    </bean>
In above for every indexproperty there is given boost value
So in above boost value score the search result means suppose you want to search text "demo" and result is match in ean, name, description and i want to see description result first then i will give higher boost value for description like below
<bean class="de.hybris.platform.commerceservices.search.solrfacetsearch.querybuilder.impl.DefaultFreeTextQueryBuilder">
                    <property name="propertyName" value="description"/>
                    <property name="boost" value="110"/>
</bean>
So Finally, In interview if you get question like suppose search text is match in more than one index property but i want to see any specific property result first then you can give answer of Solr boost

Solr boost can achieve
1. index-time boosts
2. Field Based Boosting
In hybris used Field Based Boosting - adding an optional query clause that "boosts" documents for matching an "important: true" query,


Final Conclusion about Solr Boost : If we want to improve the sale of the product we can boosting the product by some score. Based on the score the product will be displayed on search. This is called solr boost. We can do this by HMC or Back Office.