-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from folio-org/camunda-okapi-pattern-init
Camunda okapi pattern init
- Loading branch information
Showing
11 changed files
with
727 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/main/java/org/folio/rest/delegate/FolioLoginDelegate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.folio.rest.delegate; | ||
|
||
import static org.camunda.spin.Spin.JSON; | ||
|
||
import org.camunda.bpm.engine.delegate.DelegateExecution; | ||
import org.camunda.bpm.engine.impl.util.json.JSONObject; | ||
import org.camunda.spin.json.SpinJsonNode; | ||
import org.folio.rest.model.FolioLogin; | ||
import org.folio.rest.model.OkapiRequest; | ||
import org.folio.rest.service.LoginService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class FolioLoginDelegate extends AbstractRuntimeDelegate { | ||
|
||
@Autowired | ||
private LoginService loginService; | ||
|
||
@Override | ||
public void execute(DelegateExecution execution) throws Exception { | ||
log.info("Executing Folio Login Delegate"); | ||
|
||
OkapiRequest okapiRequest = new OkapiRequest(); | ||
okapiRequest.setRequestUrl("http://localhost:9130/authn/login"); | ||
okapiRequest.setRequestMethod("POST"); | ||
okapiRequest.setRequestContentType("application/json"); | ||
okapiRequest.setResponseBodyName("loginResponseBody"); | ||
okapiRequest.setResponseHeaderName("loginResponseHeader"); | ||
okapiRequest.setResponseStatusName("loginResponseStatus"); | ||
okapiRequest.setTenant("diku"); | ||
|
||
// A bit redundant, may want to create a login payload model, or eventually handle this better | ||
JSONObject jsonObject = new JSONObject(); | ||
jsonObject.put("username", "diku_admin"); | ||
jsonObject.put("password", "admin"); | ||
|
||
SpinJsonNode jsonNode = JSON(jsonObject.toString()); | ||
|
||
okapiRequest.setRequestPayload(jsonNode); | ||
log.info("json: {}", jsonObject.toString()); | ||
|
||
FolioLogin newLogin = loginService.folioLogin(okapiRequest); | ||
newLogin.setUsername("diku_admin"); | ||
log.info("NEW LOGIN: {}", newLogin); | ||
|
||
String json = JSON(newLogin).toString(); | ||
|
||
execution.setVariable("folioLogin", newLogin); | ||
execution.setVariable("loginJson", json); | ||
} | ||
|
||
} |
86 changes: 86 additions & 0 deletions
86
src/main/java/org/folio/rest/delegate/GenericOkapiRequestDelegate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package org.folio.rest.delegate; | ||
|
||
import org.camunda.bpm.engine.delegate.DelegateExecution; | ||
import org.camunda.bpm.engine.variable.Variables; | ||
import org.camunda.bpm.engine.variable.value.ObjectValue; | ||
import org.camunda.spin.json.SpinJsonNode; | ||
import org.folio.rest.model.FolioLogin; | ||
import org.folio.rest.model.OkapiRequest; | ||
import org.folio.rest.model.OkapiResponse; | ||
import org.folio.rest.service.OkapiRequestService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
import static org.camunda.spin.Spin.JSON; | ||
|
||
@Service | ||
public class GenericOkapiRequestDelegate extends AbstractRuntimeDelegate { | ||
|
||
private static final String REQUEST_URL = "requestUrl"; | ||
private static final String REQUEST_METHOD = "requestMethod"; | ||
private static final String REQUEST_PAYLOAD = "requestPayload"; | ||
private static final String REQUEST_URI_VARIABLES = "requestUriVariables"; | ||
|
||
private static final String REQUEST_CONTENT_TYPE = "requestContentType"; | ||
|
||
private static final String RESPONSE_STATUS = "responseStatusName"; | ||
private static final String RESPONSE_BODY = "responseBodyName"; | ||
private static final String RESPONSE_HEADER = "responseHeaderName"; | ||
|
||
@Value("${tenant.headerName:X-Okapi-Tenant}") | ||
private String tenantHeaderName; | ||
|
||
@Autowired | ||
private OkapiRequestService okapiRequestService; | ||
|
||
@Override | ||
public void execute(DelegateExecution execution) throws Exception { | ||
log.info("Executing Generic Okapi Request Delegate"); | ||
|
||
String tenant = execution.getTenantId(); | ||
|
||
String okapiToken= ""; | ||
if (execution.getVariable("folioLogin") != null) { | ||
FolioLogin folioLogin = (FolioLogin) execution.getVariable("folioLogin"); | ||
okapiToken = folioLogin.getxOkapiToken(); | ||
} | ||
|
||
SpinJsonNode jsonNode = JSON(execution.getVariable("okapiRequest")); | ||
|
||
// TODO: Refactor to map directly to OkapiRequest | ||
String requestUrl = jsonNode.prop(REQUEST_URL).stringValue(); | ||
String requestMethod = jsonNode.prop(REQUEST_METHOD).stringValue(); | ||
String requestContentType = jsonNode.prop(REQUEST_CONTENT_TYPE).stringValue(); | ||
String responseStatusName = jsonNode.prop(RESPONSE_STATUS).stringValue(); | ||
String responseHeaderName = jsonNode.prop(RESPONSE_HEADER).stringValue(); | ||
String responseBodyName = jsonNode.prop(RESPONSE_BODY).stringValue(); | ||
SpinJsonNode payload = jsonNode.prop(REQUEST_PAYLOAD); | ||
|
||
OkapiRequest okapiRequest = new OkapiRequest(); | ||
okapiRequest.setTenant(tenant); | ||
okapiRequest.setRequestUrl(requestUrl); | ||
okapiRequest.setRequestMethod(requestMethod); | ||
okapiRequest.setRequestContentType(requestContentType); | ||
okapiRequest.setResponseStatusName(responseStatusName); | ||
okapiRequest.setResponseHeaderName(responseHeaderName); | ||
okapiRequest.setResponseBodyName(responseBodyName); | ||
okapiRequest.setRequestPayload(payload); | ||
okapiRequest.setOkapiToken(okapiToken); | ||
|
||
log.info("JSON: {}", jsonNode); | ||
log.info("payload: {}", payload); | ||
|
||
OkapiResponse okapiResponse = okapiRequestService.okapiRestCall(okapiRequest); | ||
log.info("OKAPI RESPONSE: {}", okapiResponse); | ||
okapiResponse.setBody(null); | ||
|
||
ObjectValue response = Variables.objectValue(okapiResponse) | ||
.serializationDataFormat("application/json") | ||
.create(); | ||
|
||
execution.setVariable("okapiResponse", response); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.folio.rest.model; | ||
|
||
|
||
import java.io.Serializable; | ||
import java.util.Objects; | ||
|
||
public class FolioLogin implements Serializable { | ||
|
||
private static final long serialVersionUID = 6377886804058840280L; | ||
private String username; | ||
private String xOkapiToken; | ||
private String refreshToken; | ||
|
||
@Override | ||
public String toString() { | ||
return "FolioLogin{" + | ||
"username='" + username + '\'' + | ||
", xOkapiToken='" + xOkapiToken + '\'' + | ||
", refreshToken='" + refreshToken + '\'' + | ||
'}'; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
FolioLogin that = (FolioLogin) o; | ||
return Objects.equals(username, that.username) && | ||
Objects.equals(xOkapiToken, that.xOkapiToken) && | ||
Objects.equals(refreshToken, that.refreshToken); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
|
||
return Objects.hash(username, xOkapiToken, refreshToken); | ||
} | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public void setUsername(String username) { | ||
this.username = username; | ||
} | ||
|
||
public String getxOkapiToken() { | ||
return xOkapiToken; | ||
} | ||
|
||
public void setxOkapiToken(String xOkapiToken) { | ||
this.xOkapiToken = xOkapiToken; | ||
} | ||
|
||
public String getRefreshToken() { | ||
return refreshToken; | ||
} | ||
|
||
public void setRefreshToken(String refreshToken) { | ||
this.refreshToken = refreshToken; | ||
} | ||
|
||
public FolioLogin(String username, String xOkapiToken, String refreshToken) { | ||
this.username = username; | ||
this.xOkapiToken = xOkapiToken; | ||
this.refreshToken = refreshToken; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package org.folio.rest.model; | ||
|
||
import org.camunda.spin.json.SpinJsonNode; | ||
|
||
import java.io.Serializable; | ||
|
||
public class OkapiRequest implements Serializable { | ||
|
||
private static final long serialVersionUID = 698415949379750160L; | ||
private String requestUrl; | ||
private String requestMethod; | ||
private String requestContentType; | ||
private SpinJsonNode requestPayload; | ||
private Object[] requestUriVariables; | ||
private String tenant; | ||
private String okapiToken; | ||
private String responseStatusName; | ||
private String responseHeaderName; | ||
private String responseBodyName; | ||
|
||
public String getRequestUrl() { | ||
return requestUrl; | ||
} | ||
|
||
public void setRequestUrl(String requestUrl) { | ||
this.requestUrl = requestUrl; | ||
} | ||
|
||
public String getRequestMethod() { | ||
return requestMethod; | ||
} | ||
|
||
public void setRequestMethod(String requestMethod) { | ||
this.requestMethod = requestMethod; | ||
} | ||
|
||
public String getRequestContentType() { | ||
return requestContentType; | ||
} | ||
|
||
public void setRequestContentType(String requestContentType) { | ||
this.requestContentType = requestContentType; | ||
} | ||
|
||
public SpinJsonNode getRequestPayload() { | ||
return requestPayload; | ||
} | ||
|
||
public void setRequestPayload(SpinJsonNode requestPayload) { | ||
this.requestPayload = requestPayload; | ||
} | ||
|
||
public Object[] getRequestUriVariables() { | ||
return requestUriVariables; | ||
} | ||
|
||
public void setRequestUriVariables(Object[] requestUriVariables) { | ||
this.requestUriVariables = requestUriVariables; | ||
} | ||
|
||
public String getTenant() { | ||
return tenant; | ||
} | ||
|
||
public void setTenant(String tenant) { | ||
this.tenant = tenant; | ||
} | ||
|
||
public String getOkapiToken() { | ||
return okapiToken; | ||
} | ||
|
||
public void setOkapiToken(String okapiToken) { | ||
this.okapiToken = okapiToken; | ||
} | ||
|
||
public String getResponseStatusName() { | ||
return responseStatusName; | ||
} | ||
|
||
public void setResponseStatusName(String responseStatusName) { | ||
this.responseStatusName = responseStatusName; | ||
} | ||
|
||
public String getResponseHeaderName() { | ||
return responseHeaderName; | ||
} | ||
|
||
public void setResponseHeaderName(String responseHeaderName) { | ||
this.responseHeaderName = responseHeaderName; | ||
} | ||
|
||
public String getResponseBodyName() { | ||
return responseBodyName; | ||
} | ||
|
||
public void setResponseBodyName(String responseBodyName) { | ||
this.responseBodyName = responseBodyName; | ||
} | ||
|
||
} |
Oops, something went wrong.