Inner Error: Message: controllerFactory.createForCurrentScope is not a function - validation

I am recieving the above error in the aurelia view model
Inner Error: Message: controllerFactory.createForCurrentScope is not a
function
Here is the code,
export class UpdateClient {
public httpClient: HttpClient;
public router: Router;
public clientHelper: ClientHelper;
public validator: Validator;
public canSave: boolean;
public controller: ValidationController;
public client: Client ;
constructor(httpClient: HttpClient, router: Router, clientValidator: ClientValidator, clientHelper: ClientHelper, controllerFactory: ValidationControllerFactory, validator: Validator) {
this.httpClient = httpClient;
this.clientHelper = clientHelper;
this.router = router;
this.client = new Client
this.controller = controllerFactory.createForCurrentScope(validator);
clientValidator.validate(this.client, clientHelper);
}

You need to have #autoinject() or any other decorator on your UpdateClient class for the type metadata to be emitted correctly.
ValidationControllerFactory is registered with DI as a resolver in the module file, so simply importing that (which you have, otherwise you'd get a type error) should give you the correct thing.
You may need to double check that you have experimentalDecorators and emitDecoratorMetadata set to true in your tsconfig.json.
Also make sure you are registering the validation plugin in your main.ts like so:
aurelia.use.plugin(PLATFORM.moduleName('aurelia-validation'))

Related

NestJS Dependency Injection in Websocket Adapter

I'm trying to authenticate and check permission of a user while establishing a websocket connection in a NestJS application.
I've found this discussion which recommends to make use of NestJS Websocket adapter. You can perform the token validation in the options.allowRequest callback as below.
export class AuthenticatedSocketIoAdapter extends IoAdapter {
private readonly authService: AuthService;
constructor(private app: INestApplicationContext) {
super(app);
this.authService = app.get(AuthService);
}
createIOServer(port: number, options?: SocketIO.ServerOptions): any {
options.allowRequest = async (request, allowFunction) => {
const token = request.headers.authorization.replace('Bearer ', '');
const verified = this.authService.verifyToken(token);
if (verified) {
return allowFunction(null, true);
}
return allowFunction('Unauthorized', false);
};
return super.createIOServer(port, options);
}
}
I have a problem however with the dependency injection in the websocket adapter. The IoAdapter's constructor has an INestApplicationContext parameter from which I'm trying to get back the AuthService using app.get(AuthService) as you can see above.
The AuthService injects two other services, a UserService and the JwtService to check the JWT token. My problem is that those services remained not defined in that context.
#Injectable()
export class AuthService {
constructor(private usersService: UsersService, private jwtService: JwtService) {}
verifyToken(token: string): boolean {
// Problem: this.jwtService is undefined
const user = this.jwtService.verify(token, { publicKey });
// ... check user has permissions and return result
}
For info, the AuthService is in another module than the one which defines the Websocket. I also tried to import the AuthService (and its dependencies) in the current module but that didn't help.
Is that possible to make use the service using the app.get() method?
I could solve the DI issue by using app.resolve() instead of app.get()
export class AuthenticatedSocketIoAdapter extends IoAdapter {
private authService: AuthService;
constructor(private app: INestApplicationContext) {
super(app);
app.resolve<AuthService>(AuthService).then((authService) => {
this.authService = authService;
});
}
}
This solved the jwtService injected in the AuthService being undefined.

Not able to Use Bean method in Camel Route

This is my route class.
#Component
public class MyRoute extends RouteBuilder {
#Value("${spring.enablelog}")
public boolean enablelog;
#Value("${spring.enableroutepolicy}")
public boolean enableroutepolicy;
#Override
public void configure() throws Exception {
CronScheduleRoutePolicy routepolicy = new CronScheduleRoutePolicy();
routepolicy.setRouteStartTime("StartTime");
routepolicy.setRouteStoptTime("StopTime");
routepolicy.setRouteStartTime("StartTime");
from("activemq:queue:inputq")
.setProperty(Enablelog, constant(enablelog))
.choice().when(exchangeProperty(Enablelog).isEqualTo(True))
.bean(MyRoute.class, "setlogProperties('*', 'Request', 'Pending','Received Input message')")
.process(logProcessor).endChoice()
.process(msgProcessor).split().body()
.toD("activemq:queue:waitq")
.choice().when(exchangeProperty(Enablelog).isEqualTo(True))
.bean(MyRoute.class, "setlogProperties('*', 'Response', 'Waiting','Response message waiting to be delivered')")
.process(logProcessor).endChoice()
.end();
if (enableroutepolicy == true) {
from("activemq:queue:waitq").routePolicy(routepolicy).noAutoStartup()
.toD("activemq:queue:outputq")
.choice().when(exchangeProperty(Enablelog).isEqualTo(True))
.bean(MyRoute.class, "setlogProperties('*', 'Response', 'Success','Response message delivered')")
.process(logProcessor).endChoice()
.end();
} else {
from("activemq:queue:waitq").toD("activemq:queue:outputq")
.choice().when(exchangeProperty(Enablelog).isEqualTo(True))
.bean(MyRoute.class, "setlogProperties('*', 'Response', 'Success','Response message delivered')")
.process(logProcessor).endChoice()
.end();
}
}
public void setlogPoperties(Exchange exchange, String msgtype, String status, String statusMessage ) {
exchange.setrPoperty("MessageType", msgtype);
exchange.setrPoperty("Status",status);
exchange.setrPoperty("StatusMessage",statusMessage);
}
}
In this Route I have two routes
From InQueue to WaitQueue
From WaitQueue to OutQueue.
First route is always activated and I am activating the second route according to the requirement. In the second route I am not able to call .bean() and pass the params to the setlogPoperties(). I am new to the camel and not sure how to fix this. Any kind of help is appreciated.
Thanks,
The root cause of this problem might be that 'enablelog' and 'enableroutepolicy' are not getting picked up from your property file and as the default value of boolean is false your code is unable to reach setlogPoperties().
You have annotated the class as a #Component. So in order for #Value to retrieve values from the property file, please make sure you are either defining a #PropertySource annotated in this class itself or you are creating a bean for this component in a separate #Configuration annotated class.

HttpClient Angular 5 does not send request

I have a problem with HttpClient in Angular 5. HttpClient does not send any request (I don't see any xhr log in console) on two specified components. On the others components everything is fine.
Calling ApiService POST method (custom service which works like a wrapper for HttpClient) from Component A, but when I call this method from Component B
HttpClient seems to be frozen.
There are many components in my app that use ApiService. Everything is injected fine. I have no idea what is wrong.
--- respond
ApiService.ts
#Injectable()
export class ApiService
{
private errorListeners : Map<string, Array<(details ?: any) => any>> =
new Map<string, Array<(details ?: any) => any>>();
public constructor(private http: HttpClient)
{
}
public post<T>(path : string, data : any, urlParams : any = null) : Observable<any>
{
return this.http.post<T>(`${environment.api.path}${path}`, data, {
params: urlParams
}).catch(this.catchErrors()).map(response => {
if (response['Error']){
throw response['Error'];
}
return response;
});
}
}
--
Component
#Component({
selector: 'login-register-component',
templateUrl: './register.component.html',
styleUrls: [
'./../../assets/main/css/pages/login.css'
]
})
export class RegisterComponent implements OnInit, OnDestroy
{
public constructor(private route: ActivatedRoute,
private router: Router,
private userService : UserService,
private apiService: ApiService
)
{
this.apiService.post('/some-endpoint', null, {}).subscribe(res => {
console.log(res);
});
}
HttpClient does not work even if i directly inject HttpClient into Component
-- Other component in the same module
example call: (it works)
public loginTraditionalMethod(emailAddress : string, plainPassword : string)
{
this.apiService.post('/auth/email', {
email: emailAddress,
password: plainPassword
}, {}).subscribe(res => {
console.log(res);
})
}
I was having the same problem, no xhr request after subscribing to a http.get(). This was a request for a forgotten password function, I was therefore not connected to the app.
The request was being intercepted by an http token interceptor that was returning an empty Observable if no session was detected.
Never know, this might help someone...

Spring Boot. Different errors hanldilng for #Ccontroller and #RestController in one app

I have normal #Controller and #RestController in one App.
How it possible to handle erros in Json for REST, and redirect to error page for normal #Controller?
You can make use of the Spring Annotation #ExceptionHandler in your controller and throw Exceptions in your controller logic. For an short example I will use Java's RuntimeException. You could define your own exception and throw them
// your controller classes
#Controller
public class MyController {
#ExceptionHanlder(RuntimeException.class)
public String errorInController(){
// for your custom page
return "yourDefineErrorTemplatePage";
// if you want to redirect to the default spring page
// return "redirect:/error";
}
#RequestMapping("yourFirstEndpoint")
public String getPage(){
if(yourLogicHere){
throw new RuntimeException("Display error page");
}
return "myPage";
}
}
Your #RestController could look like the following:
#RestController
public class RestControllerClass{
#ExceptionHandler(RuntimeException.class)
public ResponseEntity<Error> errorOccured(){
// you can return just a String or define your own 'error object'
Error error = new Error("Some error occured");
return new ResponseEntity<Error>(error, Http.Status.NOT_FOUND);
}
#RequestMapping("yourSecondEndpoint")
public ResponseEntity<YourEntity> getPage(){
// the entity you want do return as json
YourEntity yourEntity = new YourEntity();
if(yourLogicHere){
throw new RuntimeException("Display error page");
}
return new ResponseEntity<YourEntity>(yourEntity, HttpStatus.OK);
}
}
Example for the Error Object:
public class Error{
private String errorMessage;
public Error(String errorMessage){
this.errorMessage = errorMessage;
}
}
I hope this small example can solve your Problem.
For more details visit: https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc

Async Spring Boot using Kotlin not working

I'm trying to create a Spring Service that performs an operation asynchronously and returns a ListenableFuture. I want the failure callback to be triggered when the operation fails - my attempt to do this is to use AsyncResult.forExecutionException as seen below:
#Service
open class UserClientService {
#Async
fun fetchUser(email: String): ListenableFuture<User> {
val uri = buildUri(email)
val headers = buildHeaders()
try {
val result = restTemplate.exchange(uri, HttpMethod.GET, HttpEntity<Any>(headers), User::class.java)
return AsyncResult.forValue(result.body)
} catch (e: RestClientException) {
return AsyncResult.forExecutionException(e)
}
}
}
The entry-point:
#SpringBootApplication
#EnableAsync
open class UserProxyApplication
fun main(args: Array<String>) {
SpringApplication.run(UserProxyApplication::class.java, *args)
}
The Spring RestController implementation is as follows:
#RestController
#RequestMapping("/users")
class UserController #Autowired constructor(
val client: UserClientService
) {
#RequestMapping(method = arrayOf(RequestMethod.GET))
fun getUser(#RequestParam(value = "email") email: String): DeferredResult<ResponseEntity<User>> {
val result = DeferredResult<ResponseEntity<User>>(TimeUnit.SECONDS.toMillis(10))
client.fetchUser(email).addCallback(
{ success -> result.setResult(ResponseEntity.ok(success)) },
{ failure -> result.setResult(ResponseEntity(HttpStatus.NOT_FOUND)) }
)
return result;
}
}
Problem is that the failure callback in the UserController is never triggered when an exception is thrown in the UserClientService REST call. Instead, the success callback is triggered with success argument being null.
In Kotlin, I can check if success is null by using success!! - this throws an exception that then does trigger the failure callback with failure argument being the NPE.
Question is how can I trigger the failure callback in the UserController when an exception has occurred in the UserClientService?
Update A it seems that everything is executed on the same thread "http-nio-8080-exec-XXX" regardless of whether I use #Async or not -- see comments.
This all works if:
A) the method fetchUser is declared open, i.e. not final so that Spring can proxy the call
...or...
B) you create an interface IUserClientService and use that in the constructor of the UserController:
interface IUserClientService {
fun fetchUser(email: String): ListenableFuture<User>
}
Now the UserClientService implements the interface:
#Service
open class UserClientService : IUserClientService {
#Async
override fun fetchUser(email: String): ListenableFuture<User> {
// ... rest as shown in question ...
And finally the UserController:
#RestController
#RequestMapping("/users")
class UserController #Autowired constructor(
val client: IUserClientService
) {
#RequestMapping(method = arrayOf(RequestMethod.GET))
fun getUser(#RequestParam(value = "email") email: String): DeferredResult<ResponseEntity<User>> {
// ... rest as shown in question ...
Not sure if this is because I'm using Kotlin. The examples that I've seen don't require implementing an interface.

Resources