# Using Java
source: https://developer.mastercard.com/consent-management/documentation/tutorials/project-setup/java-tutorial-setup/index.md

## Introduction {#introduction}

This section describes the requirements for the Java tutorial and shows how to setup
configuration for the Mastercard libraries to encrypt and sign requests.

## Dependencies {#dependencies}

* [Mastercard Developers Account](https://developer.mastercard.com/account/log-in)
* [Java 8+](https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html)
* [Apache Maven 3.3+](https://maven.apache.org/download.cgi)
* [Mastercard API signing library](https://github.com/Mastercard/oauth1-signer-java)
* [Mastercard API encryption library](https://github.com/Mastercard/client-encryption-java)

## Application Configuration {#application-configuration}

1. Create an account at [Mastercard Developers](https://developer.mastercard.com/account/sign-up).
2. Create a new project and add the `Consents` API to your project.
   * A zip file will be downloaded automatically with your keys.
3. Take note of the given **consumer key, keyalias, and keystore password** given upon the project creation.
4. Download the client **encryption key and fingerprint** given upon the project creation.
5. Download the reference app from here: [consent-java-ref-app.zip](https://static.developer.mastercard.com/content/consent-management/uploads/consent-java-ref-app.zip) (674KB)
6. Copy the downloaded `.p12` and `.pem` files to `/src/main/resources`.
7. Update the properties found in `src/main/resources/application.properties`.

```java
signing.consumerKey=
signing.pkcs12KeyFile=
signing.keyAlias=
signing.keyPassword=

client.encryption.pemFile=
```

7. Run `mvn clean compile` from the root of the project directory.
   * When compile is run, the [OpenAPI Generator plugin](https://mvnrepository.com/artifact/org.openapitools/openapi-generator-maven-plugin) will generate the sources for connecting to the Consent Manager API.
8. run `mvn spring-boot:run` to start the project.
   * to run with sandbox profile use `mvn spring-boot:run -Dspring-boot.run.profiles=sandbox`

#### Integrating with OpenAPI Generator {#integrating-with-openapi-generator}

[OpenAPI Generator](https://github.com/OpenAPITools/openapi-generator) generates API client libraries from [OpenAPI Specs](https://github.com/OAI/OpenAPI-Specification).
It provides generators and library templates for supporting multiple languages and frameworks.

##### OpenAPI Generator Plugin Configuration {#openapi-generator-plugin-configuration}

```xml
<build>
  <plugins>
    <plugin>
      <groupId>org.openapitools</groupId>
      <artifactId>openapi-generator-maven-plugin</artifactId>
      <version>3.3.4</version>
      <executions>
        <execution>
          <id>Consent Management REST Client</id>
          <goals>
            <goal>generate</goal>
          </goals>
          <configuration>
            <inputSpec>${project.basedir}/src/main/resources/api-consents-spec.yml</inputSpec>
            <generatorName>java</generatorName>
            <!-- No "library" element here means the plugin will use the default library template ("okhttp-gson") -->
            <configOptions>
              <sourceFolder>src/gen/java/main</sourceFolder>
              <dateLibrary>java8</dateLibrary>
            </configOptions>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>
```

#### See also: {#see-also}

* [OpenAPI Generator (maven Plugin)](https://mvnrepository.com/artifact/org.openapitools/openapi-generator-maven-plugin)
* [OpenAPI Generator (executable)](https://mvnrepository.com/artifact/org.openapitools/openapi-generator-cli)
* [CONFIG OPTIONS for java](https://github.com/OpenAPITools/openapi-generator/blob/master/docs/generators/java.md)

## Mastercard oauth1 Signer and Encryption Library {#mastercard-oauth1-signer-and-encryption-library}

These dependencies are required to properly call the API.

```xml
    <dependency>
        <groupId>com.mastercard.developer</groupId>
        <artifactId>oauth1-signer</artifactId>
        <version>${oauth1-signer-version}</version>
        <!-- See: https://github.com/Mastercard/oauth1-signer-java/releases -->
    </dependency>

    <dependency>
        <groupId>com.mastercard.developer</groupId>
        <artifactId>client-encryption</artifactId>
        <version>${client-encryption-version}</version>
        <!-- See: https://github.com/Mastercard/client-encryption-java/releases -->
    </dependency>
```

See the code used in this application to utilize the library.

```Java
  ApiClient client = new ApiClient();
  client.setBasePath(apiBasePath);
  client.setDebugging(true);

  File signingKey = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + pkcs12KeyFile);

  PrivateKey privateKey = AuthenticationUtils.loadSigningKey(signingKey.getPath(), keyAlias, keyPassword);

  Certificate encryptionCertificate = EncryptionUtils.loadEncryptionCertificate(
    ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + encryptionCert).getPath());

  JweConfig config = JweConfigBuilder.aJweEncryptionConfig()
    .withEncryptionCertificate(encryptionCertificate)
    .withEncryptionPath("$", "$")
    .withEncryptedValueFieldName("jweEncryptedData")
    .build();

  List<Interceptor> interceptors = client.getHttpClient().interceptors();
  interceptors.add(OkHttp2EncryptionInterceptor.from(config));
  interceptors.add(new OkHttp2OAuth1Interceptor(consumerKey, privateKey));

}
```

