Tuesday, 19 May 2020

Access modifier in java

Access modifier in java
1. default
2. private
3. protected
4. public

1.default:-

-If you dont define any access modifier for any method in java then that method will access within same package  classes defined under the package.

- if try to access method outside the package you will get error.
Note:- if class is public but method is default then again you will get error.

Note if class is not public but method is public again you will get error at package import in another package.

2.private :- private access modifier method or variable accessed within the class.If you try to access method out the class the class you will get error .
even the class in same package.
 you can not declare a class private .you will get error.

3.protected  method you cant access outside the package. You will get protected method access within the same package classes .

Note:- If you want to access protected method in another package then 
extends another package class with method class name which contain protected method.
then create the object  of current class and call protected method .

Public :- accessible every where no rstriction.
 

Wednesday, 6 May 2020

interceptor in spring boot

https://medium.com/@dila.gurung/intercepting-incoming-request-using-springs-interceptor-bc1300e03f9


registry.addInterceptor(new LogInterceptor()).addPathPatterns("/secure-code");
//addPathPattern ensure that interceptor is levied to specified pattern. In our case it is applicable to path with this pattern "/admin/login"
Similiary path can be exculded from intercepting using code given below.
" registry.addInterceptor(new LogInterceptor()).addPathPatterns("/secure-code/*").excludePathPatterns("/secure-code/public"); "


https://stackoverflow.com/questions/34970179/exclude-spring-request-handlerinterceptor-by-path-pattern



@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LocaleInterceptor());
        registry.addInterceptor(new ThemeInterceptor()).addPathPatterns("/**").excludePathPatterns("/admin/**");

        // multiple urls (same is possible for `exludePathPatterns`)
        registry.addInterceptor(new SecurityInterceptor()).addPathPatterns("/secure/*", "/admin/**", "/profile/**");
    }
}

Tuesday, 5 May 2020

download file instead of open in browser

 $s3Client = new S3Client(array(
           'version'     => 'latest',
           'region'      => env('lambda_region'),
           'credentials' => array(
               'key'    => env('s3_accessKey'),
               'secret' => env('s3_secretKey')
           )
       ));
       $result =  $s3Client->putObject(array(
           'Bucket'     => env('s3_bucket'),
           'Key'        => $fileName,
           'SourceFile' => $localfilePath,
           'ContentType' =>'image/png',
           'ContentDisposition' => 'attachment',
           'ACL'          => 'public-read'
       ));
     return $result['ObjectURL'];
   }

Friday, 1 May 2020

hibernet dependency pom file

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.doctorbest</groupId>
<artifactId>myapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>myapp</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<profiles>
        <profile>
            <id>jar</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <artifact-packaging>jar</artifact-packaging>               
            </properties>
             <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>                   
                </dependency>
            </dependencies>
        </profile>
        </profiles>

</project>