Skip to content
This repository has been archived by the owner on May 25, 2022. It is now read-only.

Commit

Permalink
feat: add basic call support for java
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Mar 28, 2022
1 parent 782ed57 commit d8c9295
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package org.archguard.scanner.sourcecode.backend

import chapi.domain.core.CodeDataStruct
import chapi.domain.core.CodeFunction
import org.archguard.scanner.sourcecode.frontend.ContainerDemand
import org.archguard.scanner.sourcecode.frontend.ContainerResource
import org.archguard.scanner.sourcecode.frontend.ContainerService

class JavaApiAnalyser {
var demands: List<ContainerDemand> = listOf()
var resources: List<ContainerResource> = listOf()

fun analysisByNode(node: CodeDataStruct, _workspace: String) {
Expand All @@ -20,6 +22,39 @@ class JavaApiAnalyser {

node.Functions.forEach { createResource(it, baseUrl, node) }
}

val useRestTemplate = node.Imports.filter { it.Source.endsWith(".RestTemplate") }
if (useRestTemplate.isNotEmpty()) {
node.Functions.forEach {
it.FunctionCalls.forEach { call ->
if (call.NodeName == "RestTemplate") {
val url = call.Parameters[0].TypeValue.removePrefix("\"").removeSuffix("\"")
var method = ""
val functionName = call.FunctionName
when {
functionName.startsWith("Get") -> {
method = "Get"
}
functionName.startsWith("Post") -> {
method = "Post"
}
functionName.startsWith("Delete") -> {
method = "Delete"
}
functionName.startsWith("Put") -> {
method = "Put"
}
}

demands = demands + ContainerDemand(
source_caller = node.NodeName,
target_url = url,
target_http_method = method
)
}
}
}
}
}

private fun createResource(func: CodeFunction, baseUrl: String, node: CodeDataStruct) {
Expand Down Expand Up @@ -70,6 +105,7 @@ class JavaApiAnalyser {

val componentRef = ContainerService(name = "")
componentRef.resources = this.resources
componentRef.demands = this.demands

componentCalls += componentRef
return componentCalls
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,19 @@ internal class JavaApiAnalyserTest {
assertEquals("/api/sub/overview", resources[0].sourceUrl)
assertEquals("Get", resources[0].sourceHttpMethod)
}

@Test
fun identRestTemplateCall() {
val resource = this.javaClass.classLoader.getResource("resttemplate/")!!
val path = Paths.get(resource.toURI()).toFile().absolutePath

val nodes = JavaAnalyserApp().analysisNodeByPath(path)
val javaApiAnalyser = JavaApiAnalyser()
nodes.forEach {
javaApiAnalyser.analysisByNode(it, "")
}

val services = javaApiAnalyser.toContainerServices()
assertEquals(1, services[0].demands.size)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.journaldev.spring.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.journaldev.spring.model.Person;

@Service
public class PersonClientImpl implements PersonClient {

@Autowired
RestTemplate restTemplate;

public List<Person> getAllPerson() {
ResponseEntity<Person[]> response = restTemplate.getForEntity("/springData/person", Person[].class);
return Arrays.asList(response.getBody());

}
}

0 comments on commit d8c9295

Please sign in to comment.