{"id":13787775,"url":"https://github.com/StyraInc/opa-java","last_synced_at":"2025-05-12T01:31:39.480Z","repository":{"id":226112387,"uuid":"767723398","full_name":"StyraInc/opa-java","owner":"StyraInc","description":"The Styra-supported driver to connect via Java to Open Policy Agent (OPA) and Enterprise OPA deployments.","archived":false,"fork":false,"pushed_at":"2025-04-24T18:33:09.000Z","size":4883,"stargazers_count":17,"open_issues_count":0,"forks_count":3,"subscribers_count":10,"default_branch":"main","last_synced_at":"2025-04-24T18:33:16.284Z","etag":null,"topics":["opa","open-policy-agent","sdk-java"],"latest_commit_sha":null,"homepage":"https://docs.styra.com/sdk","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/StyraInc.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-03-05T19:34:13.000Z","updated_at":"2025-04-24T18:32:33.000Z","dependencies_parsed_at":"2024-04-22T22:27:48.686Z","dependency_job_id":"ec31fc71-3625-4c87-8170-549c6f50c993","html_url":"https://github.com/StyraInc/opa-java","commit_stats":null,"previous_names":["styrainc/opa-java"],"tags_count":23,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StyraInc%2Fopa-java","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StyraInc%2Fopa-java/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StyraInc%2Fopa-java/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StyraInc%2Fopa-java/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/StyraInc","download_url":"https://codeload.github.com/StyraInc/opa-java/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253659392,"owners_count":21943629,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["opa","open-policy-agent","sdk-java"],"created_at":"2024-08-03T21:00:30.670Z","updated_at":"2025-05-12T01:31:39.464Z","avatar_url":"https://github.com/StyraInc.png","language":"Java","readme":"# OPA Java SDK\n\n[![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)\n[![Maven Central Version](https://img.shields.io/maven-central/v/com.styra/opa?label=Maven%20Central\u0026logo=apache-maven\u0026color=%2324b6e0)](https://central.sonatype.com/artifact/com.styra/opa)\n\n\u003e [!IMPORTANT]\n\u003e The documentation for this SDK lives at https://docs.styra.com/sdk, with reference documentation available at https://styrainc.github.io/opa-java/javadoc, and development documentation at https://styrainc.github.io/opa-java/\n\nYou can use the Styra OPA SDK to connect to [Open Policy Agent](https://www.openpolicyagent.org/) and [Enterprise OPA](https://www.styra.com/enterprise-opa/) deployments.\n\n## SDK Installation\n\nThis package is published on Maven Central as [`com.styra/opa`](https://central.sonatype.com/artifact/com.styra/opa/overview). The Maven Central page includes up-to-date instructions to add it as a dependency to your Java project, tailored for a variety of build systems including Maven and Gradle.\n\nIf you wish to build from source and publish the SDK artifact to your local Maven repository (on your filesystem) then use the following command (after cloning the git repo locally):\n\nOn Linux/MacOS:\n```bash\n./gradlew publishToMavenLocal -Pskip.signing\n```\n\nOn Windows:\n```bash\ngradlew.bat publishToMavenLocal -Pskip.signing\n```\n\n## SDK Example Usage (high-level)\n\n```java\npackage org.example;\n\nimport com.styra.opa.OPAClient;\nimport com.styra.opa.OPAException;\n\nimport java.util.Map;\nimport java.util.List;\n\nimport static java.util.Map.entry;\n\npublic class App {\n    public static void main(String[] args) throws Exception {\n        // Create an OPA instance, this handles any state needed for interacting\n        // with OPA, and can be re-used for multiple requests if needed.\n        String opaURL = \"http://localhost:8181\";\n        OPAClient opa = new OPAClient(opaURL);\n\n        // This will be the input to our policy.\n        java.util.Map\u003cString,Object\u003e input = java.util.Map.ofEntries(\n            entry(\"subject\", \"alice\"),\n            entry(\"action\", \"read\"),\n            entry(\"resource\", \"/finance/reports/fy2038_budget.csv\")\n        );\n\n        // We will read the list of policy violations, and whether the request\n        // is allowed or not into these.\n        java.util.List\u003cObject\u003e violations;\n        boolean allowed;\n\n        // Perform the request against OPA.\n        try {\n            allowed = opa.check(\"policy/allow\", input);\n            violations = opa.evaluate(\"policy/violations\", input);\n        } catch (OPAException e ) {\n            // Note that OPAException usually wraps other exception types, in\n            // case you need to do more complex error handling.\n            System.out.println(\"exception while making request against OPA: \" + e);\n            throw e; // crash the program\n        }\n\n        System.out.println(\"allowed: \" + allowed);\n        System.out.println(\"violations: \" + violations);\n    }\n}\n```\n\n\u003e [!NOTE]\n\u003e For low-level SDK usage, see the sections below.\n\n---\n\n\u003c!-- No SDK Installation [installation] --\u003e\n\n# OPA OpenApi SDK (low-level)\n\n\u003c!-- Start Summary [summary] --\u003e\n## Summary\n\nFor more information about the API: [Enterprise OPA documentation](https://docs.styra.com/enterprise-opa)\n\u003c!-- End Summary [summary] --\u003e\n\n\u003c!-- Start Table of Contents [toc] --\u003e\n## Table of Contents\n\u003c!-- $toc-max-depth=2 --\u003e\n* [OPA Java SDK](#opa-java-sdk)\n  * [SDK Installation](#sdk-installation)\n  * [SDK Example Usage (high-level)](#sdk-example-usage-high-level)\n* [OPA OpenApi SDK (low-level)](#opa-openapi-sdk-low-level)\n  * [SDK Example Usage](#sdk-example-usage)\n  * [Available Resources and Operations](#available-resources-and-operations)\n  * [Server Selection](#server-selection)\n  * [Error Handling](#error-handling)\n  * [Authentication](#authentication)\n  * [Development](#development)\n  * [Community](#community)\n\n\u003c!-- End Table of Contents [toc] --\u003e\n\n\u003c!-- Start SDK Example Usage [usage] --\u003e\n## SDK Example Usage\n\n### Example 1\n\n```java\npackage hello.world;\n\nimport com.styra.opa.openapi.OpaApiClient;\nimport com.styra.opa.openapi.models.errors.ClientError;\nimport com.styra.opa.openapi.models.errors.ServerError;\nimport com.styra.opa.openapi.models.operations.ExecuteDefaultPolicyWithInputResponse;\nimport com.styra.opa.openapi.models.shared.Input;\nimport java.lang.Exception;\n\npublic class Application {\n\n    public static void main(String[] args) throws ClientError, ServerError, Exception {\n\n        OpaApiClient sdk = OpaApiClient.builder()\n            .build();\n\n        ExecuteDefaultPolicyWithInputResponse res = sdk.executeDefaultPolicyWithInput()\n                .input(Input.of(4963.69))\n                .call();\n\n        if (res.result().isPresent()) {\n            // handle response\n        }\n    }\n}\n```\n\n### Example 2\n\n```java\npackage hello.world;\n\nimport com.styra.opa.openapi.OpaApiClient;\nimport com.styra.opa.openapi.models.errors.ClientError;\nimport com.styra.opa.openapi.models.errors.ServerError;\nimport com.styra.opa.openapi.models.operations.*;\nimport com.styra.opa.openapi.models.shared.Input;\nimport java.lang.Exception;\n\npublic class Application {\n\n    public static void main(String[] args) throws ClientError, ServerError, Exception {\n\n        OpaApiClient sdk = OpaApiClient.builder()\n            .build();\n\n        ExecutePolicyWithInputRequest req = ExecutePolicyWithInputRequest.builder()\n                .path(\"app/rbac\")\n                .requestBody(ExecutePolicyWithInputRequestBody.builder()\n                    .input(Input.of(true))\n                    .build())\n                .build();\n\n        ExecutePolicyWithInputResponse res = sdk.executePolicyWithInput()\n                .request(req)\n                .call();\n\n        if (res.successfulPolicyResponse().isPresent()) {\n            // handle response\n        }\n    }\n}\n```\n\n### Example 3\n\n```java\npackage hello.world;\n\nimport com.styra.opa.openapi.OpaApiClient;\nimport com.styra.opa.openapi.models.errors.BatchServerError;\nimport com.styra.opa.openapi.models.errors.ClientError;\nimport com.styra.opa.openapi.models.operations.*;\nimport com.styra.opa.openapi.models.shared.Input;\nimport java.lang.Exception;\nimport java.util.Map;\n\npublic class Application {\n\n    public static void main(String[] args) throws ClientError, BatchServerError, Exception {\n\n        OpaApiClient sdk = OpaApiClient.builder()\n            .build();\n\n        ExecuteBatchPolicyWithInputRequest req = ExecuteBatchPolicyWithInputRequest.builder()\n                .path(\"app/rbac\")\n                .requestBody(ExecuteBatchPolicyWithInputRequestBody.builder()\n                    .inputs(Map.ofEntries(\n                        Map.entry(\"key\", Input.of(6919.52))))\n                    .build())\n                .build();\n\n        ExecuteBatchPolicyWithInputResponse res = sdk.executeBatchPolicyWithInput()\n                .request(req)\n                .call();\n\n        if (res.batchSuccessfulPolicyEvaluation().isPresent()) {\n            // handle response\n        }\n    }\n}\n```\n\u003c!-- End SDK Example Usage [usage] --\u003e\n\n\u003c!-- Start Available Resources and Operations [operations] --\u003e\n## Available Resources and Operations\n\n\u003cdetails open\u003e\n\u003csummary\u003eAvailable methods\u003c/summary\u003e\n\n### [OpaApiClient SDK](docs/sdks/opaapiclient/README.md)\n\n* [executeDefaultPolicyWithInput](docs/sdks/opaapiclient/README.md#executedefaultpolicywithinput) - Execute the default decision  given an input\n* [executePolicy](docs/sdks/opaapiclient/README.md#executepolicy) - Execute a policy\n* [executePolicyWithInput](docs/sdks/opaapiclient/README.md#executepolicywithinput) - Execute a policy given an input\n* [executeBatchPolicyWithInput](docs/sdks/opaapiclient/README.md#executebatchpolicywithinput) - Execute a policy given a batch of inputs\n* [compileQueryWithPartialEvaluation](docs/sdks/opaapiclient/README.md#compilequerywithpartialevaluation) - Partially evaluate a query\n* [health](docs/sdks/opaapiclient/README.md#health) - Verify the server is operational\n\n\u003c/details\u003e\n\u003c!-- End Available Resources and Operations [operations] --\u003e\n\n\u003c!-- Start Server Selection [server] --\u003e\n## Server Selection\n\n### Override Server URL Per-Client\n\nThe default server can be overridden globally using the `.serverURL(String serverUrl)` builder method when initializing the SDK client instance. For example:\n```java\npackage hello.world;\n\nimport com.styra.opa.openapi.OpaApiClient;\nimport com.styra.opa.openapi.models.errors.ClientError;\nimport com.styra.opa.openapi.models.errors.ServerError;\nimport com.styra.opa.openapi.models.operations.ExecuteDefaultPolicyWithInputResponse;\nimport com.styra.opa.openapi.models.shared.Input;\nimport java.lang.Exception;\n\npublic class Application {\n\n    public static void main(String[] args) throws ClientError, ServerError, Exception {\n\n        OpaApiClient sdk = OpaApiClient.builder()\n                .serverURL(\"http://localhost:8181\")\n            .build();\n\n        ExecuteDefaultPolicyWithInputResponse res = sdk.executeDefaultPolicyWithInput()\n                .input(Input.of(4963.69))\n                .call();\n\n        if (res.result().isPresent()) {\n            // handle response\n        }\n    }\n}\n```\n\u003c!-- End Server Selection [server] --\u003e\n\n\u003c!-- Start Error Handling [errors] --\u003e\n## Error Handling\n\nHandling errors in this SDK should largely match your expectations. All operations return a response object or raise an exception.\n\nBy default, an API error will throw a `models/errors/SDKError` exception. When custom error responses are specified for an operation, the SDK may also throw their associated exception. You can refer to respective *Errors* tables in SDK docs for more details on possible exception types for each operation. For example, the `executeDefaultPolicyWithInput` method throws the following exceptions:\n\n| Error Type                | Status Code | Content Type     |\n| ------------------------- | ----------- | ---------------- |\n| models/errors/ClientError | 400, 404    | application/json |\n| models/errors/ServerError | 500         | application/json |\n| models/errors/SDKError    | 4XX, 5XX    | \\*/\\*            |\n\n### Example\n\n```java\npackage hello.world;\n\nimport com.styra.opa.openapi.OpaApiClient;\nimport com.styra.opa.openapi.models.errors.ClientError;\nimport com.styra.opa.openapi.models.errors.ServerError;\nimport com.styra.opa.openapi.models.operations.ExecuteDefaultPolicyWithInputResponse;\nimport com.styra.opa.openapi.models.shared.Input;\nimport java.lang.Exception;\n\npublic class Application {\n\n    public static void main(String[] args) throws ClientError, ServerError, Exception {\n\n        OpaApiClient sdk = OpaApiClient.builder()\n            .build();\n\n        ExecuteDefaultPolicyWithInputResponse res = sdk.executeDefaultPolicyWithInput()\n                .input(Input.of(4963.69))\n                .call();\n\n        if (res.result().isPresent()) {\n            // handle response\n        }\n    }\n}\n```\n\u003c!-- End Error Handling [errors] --\u003e\n\n\u003c!-- Start Authentication [security] --\u003e\n## Authentication\n\n### Per-Client Security Schemes\n\nThis SDK supports the following security scheme globally:\n\n| Name         | Type | Scheme      |\n| ------------ | ---- | ----------- |\n| `bearerAuth` | http | HTTP Bearer |\n\nYou can set the security parameters through the `security` builder method when initializing the SDK client instance. For example:\n```java\npackage hello.world;\n\nimport com.styra.opa.openapi.OpaApiClient;\nimport com.styra.opa.openapi.models.errors.ClientError;\nimport com.styra.opa.openapi.models.errors.ServerError;\nimport com.styra.opa.openapi.models.operations.ExecuteDefaultPolicyWithInputResponse;\nimport com.styra.opa.openapi.models.shared.Input;\nimport com.styra.opa.openapi.models.shared.Security;\nimport java.lang.Exception;\n\npublic class Application {\n\n    public static void main(String[] args) throws ClientError, ServerError, Exception {\n\n        OpaApiClient sdk = OpaApiClient.builder()\n                .security(Security.builder()\n                    .bearerAuth(\"\u003cYOUR_BEARER_TOKEN_HERE\u003e\")\n                    .build())\n            .build();\n\n        ExecuteDefaultPolicyWithInputResponse res = sdk.executeDefaultPolicyWithInput()\n                .input(Input.of(4963.69))\n                .call();\n\n        if (res.result().isPresent()) {\n            // handle response\n        }\n    }\n}\n```\n\u003c!-- End Authentication [security] --\u003e\n\n\u003c!-- Placeholder for Future Speakeasy SDK Sections --\u003e\n\n## Development\n\nThis repository includes components generated by [Speakeasy](https://www.speakeasyapi.dev/) based on [this OpenAPI spec](https://github.com/StyraInc/enterprise-opa/tree/main/openapi), as well as human authored code that simplifies usage. The Speakeasy generated code resides in the [com.styra.opa.sdk](https://styrainc.github.io/opa-java/javadoc/com/styra/opa/sdk/package-summary.html) package and offers the greatest level of control, but is more verbose and complicated. The hand written [com.styra.opa](https://styrainc.github.io/opa-java/javadoc/com/styra/opa/package-summary.html) package offers a simplified API designed to make using the OPA REST API straightforward for common use cases.\n\n### Build Instructions\n\n**To build the SDK**, use `./gradlew build`, the resulting JAR will be placed in `./build/libs/api.jar`.\n\n**To build the documentation** site, including JavaDoc, run `./scripts/build_docs.sh OUTPUT_DIR`. You should replace `OUTPUT_DIR` with a directory on your local system where you would like the generated docs to be placed. You can also preview the documentation site ephemerally using `./scripts/serve_docs.sh`, which will serve the docs on `http://localhost:8000` until you use Ctrl+C to exit the script.\n\n**To run the unit tests**, you can use `./gradlew test`.\n\n**To run the linter**, you can use `./gradlew lint`\n\n## Community\n\nFor questions, discussions and announcements related to Styra products, services and open source projects, please join\nthe Styra community on [Slack](https://communityinviter.com/apps/styracommunity/signup)!\n","funding_links":[],"categories":["Language and Platform Integrations"],"sub_categories":["Java"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FStyraInc%2Fopa-java","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FStyraInc%2Fopa-java","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FStyraInc%2Fopa-java/lists"}