How to make queryparams mandatory in Java Jersey REST services? - jersey

I have a REST API that accepts 3 query params. When the query is called without any one of the query parameters, the API executes and returns the result. How do we make the queryparams mandatory? How can I add validation to check if all the parameters are present? Also, please let me know the best approach.

On a very simple level you could just inject the HttpServletRequest and check yourself:
#GET
public Response example(#Context HttpServletRequest request,
#QueryParam("name") String name) {
if (null == request.getParameter("name")) {
ResponseBuilder builder = Response.status(404);
return builder.build();
}
// Do something with name
}
Or you can implement something more elaborate using AOP. Here's a blog post about further options.

jersey doesn't give a mandatory parameter checking functionality out of the box. however you can do something like implementing your own annotation to achieve it.
Below is the annotation code:
#Target(value = ElementType.METHOD)
#Retention(value = RetentionPolicy.RUNTIME)
public #interface Required {
String[] value();
}
You also need a filter, below is the code:
public class RequiredParamResourceFilterFactory implements ResourceFilterFactory {
#Context
private transient HttpServletRequest servletRequest;
private class RequiredParamFilter implements ResourceFilter, ContainerRequestFilter {
private final String[] requiredParams;
protected List<String> parametersValueMissing;
private RequiredParamFilter(String[] requiredParams) {
this.requiredParams = requiredParams;
}
#Override
public ContainerRequest filter(ContainerRequest containerRequest) {
boolean missingMandatoryParameter = false;
List<String> missingParameters = new ArrayList<String>();
List<String> requiredParametersValueMissing = new ArrayList<String>();
List<String> URLParameters = getURLParameters(containerRequest.getQueryParameters());
List<String> methodRequiredParameters = Arrays.asList(requiredParams);
if (methodRequiredParameters != null) {
for (String methodRequiredParam : methodRequiredParameters) {
if (URLParameters == null) {
missingMandatoryParameter = true; //we will check this flag before returning result set to caller
missingParameters.add(methodRequiredParam);
} else if (!URLParameters.contains(methodRequiredParam)) {
missingMandatoryParameter = true; //we will check this flag before returning result set to caller
missingParameters.add(methodRequiredParam);
//Add to required parameters value missing List, only if the parameter is mandatory and value is not provided
// in the URL
} else if (parametersValueMissing.contains(methodRequiredParam)) {
requiredParametersValueMissing.add(methodRequiredParam);
}
}
if (missingMandatoryParameter && requiredParametersValueMissing.size() > 0) {
throw new YourCustomException("Missing Parameters = " + StringHelper.ArrayToString(missingParameters) +
"\nParameter value missing for " + StringHelper.ArrayToString(requiredParametersValueMissing));
} else if (missingMandatoryParameter) {
throw new YourCustomException("Missing Parameters = " + StringHelper.ArrayToString(missingParameters), MisbarErrorCode.VALIDATION_WRONG_INPUT_ERROR, "Customers");
} else if (requiredParametersValueMissing != null &&
requiredParametersValueMissing.size() > 0) {
throw new YourCustomException("Parameter value missing for " + StringHelper.ArrayToString(requiredParametersValueMissing));
}
}
return containerRequest;
}
#Override
public ContainerRequestFilter getRequestFilter() {
return this;
}
#Override
public ContainerResponseFilter getResponseFilter() {
return null;
}
/**
* To fetch the parameters sent to webservice call, these will be used to find if required parameter
* are present or not
*
* #param queryParams the queryparams sent
* #return all the parameters sent in URL
*/
private List<String> getURLParameters(MultivaluedMap<String,String> queryParams) {
parametersValueMissing = new ArrayList<String>();
List<String> arr = new ArrayList<String>();
for(String key:queryParams.keySet())
{
arr.add(key);
if(queryParams.get(key)==null)
parametersValueMissing.add(key);
}
if(!arr.isEmpty())
return arr;
return null;
}
}
#Override
public List<ResourceFilter> create(AbstractMethod am) {
Required required = am.getAnnotation(Required.class);
if(required!=null)
{
return Collections.<ResourceFilter>singletonList(new RequiredParamFilter(required.value()));
}
return null;
}
}
Below sample shows how to use this annotation, so in below webservice; file_id and count are mandatory parameters:
#GET
#Produces(MediaType.APPLICATION_JSON+";charset=utf-8")
#Cacheable(isCacheable = true)
#Path("posts/clusters")
#Required({"file_id","count"})
#Timed
public Response getClusters(
#QueryParam("file_id") Integer fileId,
#QueryParam("count") Integer count,
#DefaultValue("-1")#QueryParam("start_time") Long startTime){
;
}
If mandatory parameters are not provided in webservice call, you receive an error like below, mentioning the parameter names that are missing:
{
message: "Missing Parameters = file_id, count",
errorCode: "600"
}
Hope this solves your problem.

Related

Spring Boot custom annotation design not working

I am following a tutorial by PacktPublishing where some annotations are used in an example,
but the code is from 2018 and there have probably been some changes.
Spring does not recognize the Annotation when creating a bean.
Specifically, here is an annotation design that just does not work for me locally:
link
Some important code snippets are:
#Target({ElementType.TYPE})
#Retention(RetentionPolicy.RUNTIME)
#Documented
#Component
public #interface ChannelHandler {
/**
* Channel patter, alias of value()
*/
String pattern() default "";
/**
* The channel pattern that the handler will be mapped to by {#link WebSocketRequestDispatcher}
* using Spring's {#link org.springframework.util.AntPathMatcher}
*/
String value() default "";
}
#ChannelHandler("/board/*")
public class BoardChannelHandler {
private static final Logger log = LoggerFactory.getLogger(BoardChannelHandler.class);
#Action("subscribe")
public void subscribe(RealTimeSession session, #ChannelValue String channel) {
log.debug("RealTimeSession[{}] Subscribe to channel `{}`", session.id(), channel);
SubscriptionHub.subscribe(session, channel);
}
#Action("unsubscribe")
public void unsubscribe(RealTimeSession session, #ChannelValue String channel) {
log.debug("RealTimeSession[{}] Unsubscribe from channel `{}`", session.id(), channel);
SubscriptionHub.unsubscribe(session, channel);
}
}
#Target({ElementType.METHOD})
#Retention(RetentionPolicy.RUNTIME)
#Documented
public #interface Action {
/**
* The action pattern. It needs to be an exact match.
* <p>For example, "subscribe"
*/
String value() default "";
}
Can you see what the issue is here? Is there some other annotation missing for newer versions
of Spring?
UPDATE - adding other necessary code.
public class ChannelHandlerInvoker {
private static final Logger log = LoggerFactory.getLogger(ChannelHandlerInvoker.class);
private static final AntPathMatcher antPathMatcher = new AntPathMatcher();
private String channelPattern;
private Object handler;
// Key is the action, value is the method to handle that action
private final Map<String, Method> actionMethods = new HashMap<>();
public ChannelHandlerInvoker(Object handler) {
Assert.notNull(handler, "Parameter `handler` must not be null");
Class<?> handlerClass = handler.getClass();
ChannelHandler handlerAnnotation = handlerClass.getAnnotation(ChannelHandler.class);
Assert.notNull(handlerAnnotation, "Parameter `handler` must have annotation #ChannelHandler");
Method[] methods = handlerClass.getMethods();
for (Method method : methods) {
Action actionAnnotation = method.getAnnotation(Action.class);
if (actionAnnotation == null) {
continue;
}
String action = actionAnnotation.value();
actionMethods.put(action, method);
log.debug("Mapped action `{}` in channel handler `{}#{}`", action, handlerClass.getName(), method);
}
this.channelPattern = ChannelHandlers.getPattern(handlerAnnotation);
this.handler = handler;
}
public boolean supports(String action) {
return actionMethods.containsKey(action);
}
public void handle(IncomingMessage incomingMessage, RealTimeSession session) {
Assert.isTrue(antPathMatcher.match(channelPattern, incomingMessage.getChannel()), "Channel of the handler must match");
Method actionMethod = actionMethods.get(incomingMessage.getAction());
Assert.notNull(actionMethod, "Action method for `" + incomingMessage.getAction() + "` must exist");
// Find all required parameters
Class<?>[] parameterTypes = actionMethod.getParameterTypes();
// All the annotations for each parameter
Annotation[][] allParameterAnnotations = actionMethod.getParameterAnnotations();
// The arguments that will be passed to the action method
Object[] args = new Object[parameterTypes.length];
try {
// Populate arguments
for (int i = 0; i < parameterTypes.length; i++) {
Class<?> parameterType = parameterTypes[i];
Annotation[] parameterAnnotations = allParameterAnnotations[i];
// No annotation applied on this parameter
if (parameterAnnotations.length == 0) {
if (parameterType.isInstance(session)) {
args[i] = session;
} else {
args[i] = null;
}
continue;
}
// Only use the first annotation applied on the parameter
Annotation parameterAnnotation = parameterAnnotations[0];
if (parameterAnnotation instanceof Payload) {
Object arg = JsonUtils.toObject(incomingMessage.getPayload(), parameterType);
if (arg == null) {
throw new IllegalArgumentException("Unable to instantiate parameter of type `" +
parameterType.getName() + "`.");
}
args[i] = arg;
} else if (parameterAnnotation instanceof ChannelValue) {
args[i] = incomingMessage.getChannel();
}
}
actionMethod.invoke(handler, args);
} catch (Exception e) {
String error = "Failed to invoker action method `" + incomingMessage.getAction() +
"` at channel `" + incomingMessage.getChannel() + "` ";
log.error(error, e);
session.error(error);
}
}
}
#Component
public class ChannelHandlerResolver {
private static final Logger log = LoggerFactory.getLogger(ChannelHandlerResolver.class);
private static final AntPathMatcher antPathMatcher = new AntPathMatcher();
// The key is the channel ant-like path pattern, value is the corresponding invoker
private final Map<String, ChannelHandlerInvoker> invokers = new HashMap<>();
private ApplicationContext applicationContext;
public ChannelHandlerResolver(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
this.bootstrap();
}
public ChannelHandlerInvoker findInvoker(IncomingMessage incomingMessage) {
ChannelHandlerInvoker invoker = null;
Set<String> pathPatterns = invokers.keySet();
for (String pathPattern : pathPatterns) {
if (antPathMatcher.match(pathPattern, incomingMessage.getChannel())) {
invoker = invokers.get(pathPattern);
}
}
if (invoker == null) {
return null;
}
return invoker.supports(incomingMessage.getAction()) ? invoker : null;
}
private void bootstrap() {
log.info("Bootstrapping channel handler resolver");
Map<String, Object> handlers = applicationContext.getBeansWithAnnotation(ChannelHandler.class);
for (String handlerName : handlers.keySet()) {
Object handler = handlers.get(handlerName);
Class<?> handlerClass = handler.getClass();
ChannelHandler handlerAnnotation = handlerClass.getAnnotation(ChannelHandler.class);
String channelPattern = ChannelHandlers.getPattern(handlerAnnotation);
if (invokers.containsKey(channelPattern)) {
throw new IllegalStateException("Duplicated handlers found for chanel pattern `" + channelPattern + "`.");
}
invokers.put(channelPattern, new ChannelHandlerInvoker(handler));
log.debug("Mapped channel `{}` to channel handler `{}`", channelPattern, handlerClass.getName());
}
}
}
<!-- begin snippet: js hide: false console: true babel: false -->
UPDATE 2
I have managed to make ChannelHandler and Action annotations work by adding #Inherited annotation and using AnnotationUtils.findAnnotation() which traverses its super methods if the annotation is not directly present on the given method itself.
However, I haven't managed to access custom annotation value of type parameter (ChannelValue)
Here, Annotation[][] allParameterAnnotations = actionMethod.getParameterAnnotations();
returns null value.
UPDATE 3 -> SOLVED
Just add #Aspect annotation to your ChannelHandler implementation (e.g.
"BoardChannelHandler").
Looks like bootstrap() method, that goes through all the #ChannelHandler annotated beans is executed too early - try to debug it to check if it detects any beans at this stage.
If not try calling bootstrap() after Spring context is ready (for example listen for ContextRefreshedEvent.

POST Multipart file as form data to a REST service

I am trying to POST multipart file as a form data to a REST service which returns me a url after it saved at REST service end. In postman the request look like this.
I have a Spring Boot service which has a method to get Multipart file form frontend via jquery fileuploader. I need to post the file to the above URL which postman sends and saves in there end. I think i have to construct form data in my Spring boot service. Below are few snaps of the Spring boot service.
Controller end.
#RequestMapping(method = RequestMethod.POST, value = "/file-upload/{profileName:.+}")
public Attachment uploadFile(#RequestParam("file") MultipartFile input,
#PathVariable("profileName") String profileName) throws IOException {
Attachment attachment = new Attachment();
if (input != null) {
log.info("Upload a new attachment item" + input.getName());
byte[] fileByteArray = input.getBytes();
attachment.setEncodedFile(Utils.encodeBytes(fileByteArray));
attachment.setFileName(input.getOriginalFilename());
socialMediaService.uploadMedia(input, profileName);
}
return attachment;
}
SocialMediaService
public String uploadMedia(MultipartFile input, String profileName) {
String mediaUploadPath = "wall_attach/lenne-public";
Map < String, String > params = new HashMap < > ();
String mediaUploadFullPath =
UrlBuilder.build(System.getenv(Constants.HUBZILLA_URL), mediaUploadPath, params);
if (!isRestServiceProvided) {
restService = new RestService(RequestType.POST, mediaUploadFullPath);
}
MultipartEntityBuilder builder = restService.getEntityBuilder();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
try {
builder.addBinaryBody("userfile", input.getBytes(), ContentType.DEFAULT_BINARY, input.getOriginalFilename());
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
String strResp = restService.execute(profileName, Constants.HUBZILLA_PW);
return strResp;
}
return null;
}
RestService class
public class RestService {
private Logger log;
private HttpClient client = null;
private HttpRequest request = null;
private RequestType reqType = null;
private String body;
MultipartEntityBuilder builder = null;
public RestService() {
this.log = LoggerFactory.getLogger(RestService.class);
}
/**
* Create REST service with external parameters.
*
* #param reqType RequestType
* #param client HttpClient
* #param request External HttpRequest
*/
public RestService(RequestType reqType, HttpClient client, HttpRequest request, Logger log) {
this.reqType = reqType;
this.client = client;
this.request = request;
this.log = log;
}
/**
* Create REST service string parameters.
*
* #param reqType RequestType
* #param fullPath Full path of REST service
*/
public RestService(RequestType reqType, String fullPath) {
this.client = HttpClientBuilder.create().build();
this.reqType = reqType;
this.log = LoggerFactory.getLogger(RestService.class);
if (reqType == RequestType.GET) {
this.request = new HttpGet(fullPath);
} else if (reqType == RequestType.POST) {
this.request = new HttpPost(fullPath);
} else if (reqType == RequestType.DELETE) {
this.request = new HttpDelete(fullPath);
}
}
/**
* Execute REST service without authentication.
*
* #return - Result of the service.
*/
public String execute() {
return execute(null, null);
}
/**
* Execute REST web service with basic authentication.
*
* #return - Result of the service.
*/
public String execute(String user, String password) {
try {
if (user != null && password != null) {
StringBuilder authString = new StringBuilder();
authString.append(user).append(":").append(password);
String authBase = new String(
Base64.getEncoder().encode(authString.toString().getBytes(Charset.forName("UTF-8"))));
String authType = "Basic ";
String authHeader = authType + authBase;
request.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
}
HttpResponse response = null;
if (this.reqType == RequestType.GET) {
HttpGet get = (HttpGet) request;
response = client.execute(get);
} else if (this.reqType == RequestType.POST) {
HttpPost post = (HttpPost) request;
if (body != null) {
StringEntity stringEntity = new StringEntity(body);
post.setEntity(stringEntity);
}
if (builder != null) {
HttpEntity entity = builder.build();
post.setEntity(entity);
}
response = client.execute(post);
} else {
throw new NotImplementedException();
}
if (response != null && (response.getStatusLine().getStatusCode() == Status.OK.getStatusCode()
|| response.getStatusLine().getStatusCode() == Status.CREATED.getStatusCode())) {
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity);
}
} catch (Exception e) {
log.error("External service call failed ", e);
}
return null;
}
public void setBody(String body) {
this.body = body;
}
public MultipartEntityBuilder getEntityBuilder() {
this.builder = MultipartEntityBuilder.create();
return this.builder;
}
}
My problem is not getting any result after I executed the rest service upload media method. But it worked perfectly via postman.
Can anybody let me know what am I missing? Is the way I constructed the form data in java correct?
Thank you in advance.
Try adding consumes parameter in #RequestMapping like this (consumes = "multipart/form-data")
#RequestMapping(method = RequestMethod.POST, consumes = "multipart/form-data" ,value = "/file-upload/{profileName:.+}")
public Attachment uploadFile(#RequestParam("file") MultipartFile input,
#PathVariable("profileName") String profileName) throws IOException {
----
----
}
There is another relevant issue here:
Trying to upload MultipartFile with postman
Please read the comments under answer in this link.
Hope it helps!

How to access CSV data set config in my Java Request Sampler

I have to read data from a csv file which contains 10000+ records.
I want to use this data in JMeter to hit a web service.
I had written my code with the hard coded value. But I want to make it dynamic.
How can I access CSV data set config in my custom Java Request Sampler...?
How can I access the variable i declared in the CSV data set config in my java request sampler..?
Here is my full code :
#Override
public SampleResult runTest(JavaSamplerContext arg0)
{
SampleResult result = new SampleResult();
boolean success = true;
byte arr[] = new byte[] {1,49,45,1,2,(byte)214,1,1,98,0,6,0,0,9,24,0,0,0,0,0,0,0,0,0,0,0,0,0,127,(byte)255,0,21,0,16,0,75,1,0,0,58,32,2,7,0,0,4,4,0,85,81,98,0,5,14,(byte)158,0,2,0,0,0,0,0,88,82,50,69,49,83,49,86,48,67,48,0,0,1,97,75,0,84,30,12,7,17,5,7,50,0,0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,0,4,6,0,0,48,49,48,48,49,48,51,48,0,0,0,0,0,0,0,0,0,0,70,48,10,29,22,85,0,1,(byte)134,(byte)160,(byte)255,(byte)255,(byte)158,(byte)170,0,0,0,67,0,0,0,0,2,0,0,12,0,12,0,12,0,12,0,13,0,12,0,13,0,12,0,13,0,12,0,13,0,13,0,12,0,12,0,12,0,12,0,13,0,12,0,13,0,13,0,12,0,13,0,13,0,12,0,13,0,13,0,12,0,13,0,13,0,13,0,12,0,13,0,13,0,13,0,14,0,13,0,12,0,13,0,13,0,13,0,13,0,12,0,13,0,13,0,13,0,14,0,13,0,13,0,13,0,12,0,13,0,13,2,(byte)158,2,(byte)159,2,(byte)241,2,(byte)234,5,48,5,68,8,90,7,(byte)193,6,15,4,10,3,100,4,(byte)224,7,47,6,72,4,(byte)170,4,4,4,7,5,16,6,107,6,114,5,(byte)195,4,(byte)179,2,(byte)198,0,13,0,13,0,13,0,14,0,13,0,13,0,14,0,13,0,14,0,13,0,13,0,14,0,13,0,13,0,14,0,13,0,14,0,13,0,14,0,13,0,14,0,13,0,13,0,101,0,99,0,(byte)129,0,(byte)129,2,81,2,(byte)224,1,(byte)153,0,(byte)30,0,31,0,14,0,13,0,14,0,13,0,14,0,14,0,13,0,14,0,13,0,14,0,14,0,21,0,86,0,98,0,51,0,72,0,104,0,(byte)144,0,(byte)175,0,(byte)174,0,(byte)174,2,20,4,(byte)132,4,103,5,96,0,126,0,14,0,14,0,14,0,14,0,14,0,15,0,14,0,14,0,14,0,14,0,14,0,85,1,41,1,104,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,13,0,14,0,13,0,14,0,13,0,14,0,13,0,14,0,14,0,13,0,14,0,13,0,13,0,13,0,13,0,13,0,12,0,13,0,13,0,14,0,13,0,13,0,13,0,13,0,13,0,13,0,13,0,14,0,13,0,13,0,14,0,13,0,13,0,14,0,13,0,13,0,13,0,13,0,13,0,(byte)226,0,(byte)223,0,(byte)223,0,15,0,14,0,13,0,115,0,(byte) 223,(byte)162,40,38,85,115,101,114,78,97,109,101,61,101,82,101,103,38,85,115,101,114,80,97,115,115,119,111,114,100,61,97,98,99,49,50,51};
try
{
URL obj = new URL(POST_URL);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
result.sampleStart();
OutputStream os = con.getOutputStream();
os.write(arr);
os.flush();
os.close();
result.sampleEnd();
int responseCode = con.getResponseCode();
System.out.println("POST Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK)
{ //success
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null)
{
response.append(inputLine);
}
in.close();
// print result
System.out.println(response.toString().getBytes());
}
else
{
System.out.println("POST request not worked");
}
}
catch(Exception E)
{
}
//
result.setSuccessful(success);
return result;
}
#Override
public Arguments getDefaultParameters()
{
Arguments dp=new Arguments();
return dp;
}
#Override
public void setupTest(JavaSamplerContext context) {}
#Override
public void teardownTest(JavaSamplerContext context) {
}
Normally you should be able to access JMeter Variables like:
String myVar = JMeterContextService.getContext().getVariables().get("your_variable_name_here");
However if you don't want to have it hard-coded you might consider moving the configuration to Java Request Sampler GUI like:
String valueFromCsv = "";
String defaultValue = "insert_jmeter_variable_here";
#Override
public Arguments getDefaultParameters() {
Arguments dp = new Arguments();
dp.addArgument("hexData", "insert_jmeter_variable_here");
return dp;
}
#Override
public void setupTest(JavaSamplerContext context) {
valueFromCsv = context.getParameter("hexData", defaultValue );
}
This way you will be able to control the parameter value directly from JMeter GUI.
References:
JMeterContextService JavaDoc
Java Request Sampler documentation
Extending JMeter
Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For!
A full java sampler solution with CSV data:
courtesy - https://dzone.com/articles/implement-custom-jmeter-samplers
Created a class
public class VDCSampler extends AbstractJavaSamplerClient implements Serializable {
private static final String ARG1_IDATE = "idate";
private String attrib1;
#Override
public Arguments getDefaultParameters() {
Arguments defaultParameters = new Arguments();
defaultParameters.addArgument(ARG1_IDATE, attrib1);
return defaultParameters;
}
#Override
public void setupTest(JavaSamplerContext javaSamplerContext) {
attrib1 = javaSamplerContext.getParameter(ARG1_IDATE, attrib1);
}
#Override
public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
VDCFunctionalitySampling functionalityForSampling = new VDCFunctionalitySampling();
SampleResult sampleResult = new SampleResult();
sampleResult.sampleStart();
try {
String message = functionalityForSampling.testFunction(attrib1);
}
}

How to get a list of all the headers for logging in web api

I want to log all the request headers. I already have a filter like so.
Now how do I get all the request header so that I can log them?
public class LogApiFilter : AbstractActionFilter
{
private readonly ILog m_Log;
public override bool AllowMultiple
{
get
{
return true;
}
}
public LogApiFilter(ILog iLog)
{
if (iLog == null)
throw new ArgumentNullException("log instance injected is null.");
m_Log = iLog;
}
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
base.OnActionExecuted(actionExecutedContext);
}
public override void OnActionExecuting(HttpActionContext context)
{
m_Log.Debug("Web api Controller Name and Action method Name: "
+ context.ActionDescriptor.ControllerDescriptor.ControllerName
+ ", " + context.ActionDescriptor.ActionName);
base.OnActionExecuting(context);
}
}
Ok, for my own records and for others, I have comeup with this. Please do suggest if there is a better way.
private string GetRequestHeaders(HttpActionContext context)
{
// Note you can replace the type names sucha as string, HttpRequestHeaders, List<KeyValuePair<string, IEnumerable<string>>>
// with var keyword where ever possible for readability.
string headerString = string.Empty;
HttpRequestHeaders requestHeaders = context.Request.Headers;
List<KeyValuePair<string, IEnumerable<string>>> headerList = requestHeaders.ToList();
foreach (var header in headerList)
{
string key = header.Key;
List<string> valueList = header.Value.ToList();
string valueString = string.Empty;
foreach (var v in valueList)
{
valueString = valueString + v + "-";
}
headerString = headerString + key + ": " + valueString + Environment.NewLine;
}
return headerString;
}
The above method can be called from the action filter in the question. I am calling it from the method, OnActionExecuting(HttpActionContext context).
I am using ninject for di, so this is how I have configured it.
kernel.BindHttpFilter<LogApiFilter>(System.Web.Http.Filters.FilterScope.Global);
kernel.BindHttpFilter<ApiExceptionFilterAttribute>(System.Web.Http.Filters.FilterScope.Global);
kernel.BindFilter<LogMvcFilter>(System.Web.Mvc.FilterScope.Global, 0);

Trying to call code in my controller but getting Null Reference error

Don't want to over-complicate the issue, but I think I need to post all the code that's hooked into this error.
Using MvcMailer and introduced a separate Send mechanism (for use with Orchard CMS' own EMail).
The MvcMailer Code:
1) AskUsMailer.cs:
public class AskUsMailer : MailerBase, IAskUsMailer
{
public AskUsMailer()
: base()
{
//MasterName = "_Layout";
}
public virtual MvcMailMessage EMailAskUs(AskUsViewModel model)
{
var mailMessage = new MvcMailMessage { Subject = "Ask Us" };
ViewData.Model = model;
this.PopulateBody(mailMessage, viewName: "EMailAskUs");
return mailMessage;
}
}
2) IAskUsMailer.cs:
public interface IAskUsMailer : IDependency
{
MvcMailMessage EMailAskUs(AskUsViewModel model);
}
3) AskUsController.cs: (GETTING NULL REFERENCE ERROR BELOW)
[Themed]
public ActionResult Submitted()
{
//This is the new call (see new code below):
//Note: Debugging steps through eMailMessagingService,
//then shows the null reference error when continuing to
//SendAskUs
eMailMessagingService.SendAskUs(askUsData);
//Below is normal MvcMailer call:
//AskUsMailer.EMailAskUs(askUsData).Send();
return View(askUsData);
}
Note: askUsData is defined in a separate block in the controller:
private AskUsViewModel askUsData;
protected override void OnActionExecuting(ActionExecutingContext
filterContext)
{
var serialized = Request.Form["askUsData"];
if (serialized != null) //Form was posted containing serialized data
{
askUsData = (AskUsViewModel)new MvcSerializer().
Deserialize(serialized, SerializationMode.Signed);
TryUpdateModel(askUsData);
}
else
askUsData = (AskUsViewModel)TempData["askUsData"] ??
new AskUsViewModel();
TempData.Keep();
}
protected override void OnResultExecuted(ResultExecutedContext
filterContext)
{
if (filterContext.Result is RedirectToRouteResult)
TempData["askUsData"] = askUsData;
}
I did not know how to get my EMailMessagingService.cs (see below) call into the controller, so in a separate block in the controller I did this:
private IEMailMessagingService eMailMessagingService;
public AskUsController(IEMailMessagingService eMailMessagingService)
{
this.eMailMessagingService = eMailMessagingService;
}
I think this is part of my problem.
Now, the new code trying to hook into Orchard's EMail:
1) EMailMessagingServices.cs:
public class EMailMessagingService : IMessageManager
{
private IAskUsMailer askUsMailer;
private IOrchardServices orchardServices;
public EMailMessagingService(IAskUsMailer askUsMailer,
IOrchardServices orchardServices)
{
this.orchardServices = orchardServices;
this.askUsMailer = askUsMailer;
this.Logger = NullLogger.Instance;
}
public ILogger Logger { get; set; }
public void SendAskUs(AskUsViewModel model)
{
var messageAskUs = this.askUsMailer.EMailAskUs(model);
messageAskUs.To.Add("email#email.com");
//Don't need the following (setting up e-mails to send a copy anyway)
//messageAskUs.Bcc.Add(AdminEmail);
//messageAskUs.Subject = "blabla";
Send(messageAskUs);
}
....
}
The EMailMessagingService.cs also contains the Send method:
private void Send(MailMessage messageAskUs)
{
var smtpSettings = orchardServices.WorkContext.
CurrentSite.As<SmtpSettingsPart>();
// can't process emails if the Smtp settings have not yet been set
if (smtpSettings == null || !smtpSettings.IsValid())
{
Logger.Error("The SMTP Settings have not been set up.");
return;
}
using (var smtpClient = new SmtpClient(smtpSettings.Host,
smtpSettings.Port))
{
smtpClient.UseDefaultCredentials =
!smtpSettings.RequireCredentials;
if (!smtpClient.UseDefaultCredentials &&
!String.IsNullOrWhiteSpace(smtpSettings.UserName))
{
smtpClient.Credentials = new NetworkCredential
(smtpSettings.UserName, smtpSettings.Password);
}
if (messageAskUs.To.Count == 0)
{
Logger.Error("Recipient is missing an email address");
return;
}
smtpClient.EnableSsl = smtpSettings.EnableSsl;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
messageAskUs.From = new MailAddress(smtpSettings.Address);
messageAskUs.IsBodyHtml = messageAskUs.Body != null &&
messageAskUs.Body.Contains("<") &&
messageAskUs.Body.Contains(">");
try
{
smtpClient.Send(messageAskUs);
Logger.Debug("Message sent to {0} with subject: {1}",
messageAskUs.To[0].Address, messageAskUs.Subject);
}
catch (Exception e)
{
Logger.Error(e, "An unexpected error while sending
a message to {0} with subject: {1}",
messageAskUs.To[0].Address, messageAskUs.Subject);
}
}
}
Now, in EMailMessagingService.cs I was getting an error that things weren't being implemented, so I auto-generated the following (don't know if this is part of my error):
public void Send(Orchard.ContentManagement.Records.ContentItemRecord recipient, string type, string service, System.Collections.Generic.Dictionary<string, string> properties = null)
{
throw new NotImplementedException();
}
public void Send(System.Collections.Generic.IEnumerable<Orchard.ContentManagement.Records.ContentItemRecord> recipients, string type, string service, System.Collections.Generic.Dictionary<string, string> properties = null)
{
throw new NotImplementedException();
}
public void Send(System.Collections.Generic.IEnumerable<string> recipientAddresses, string type, string service, System.Collections.Generic.Dictionary<string, string> properties = null)
{
throw new NotImplementedException();
}
public bool HasChannels()
{
throw new NotImplementedException();
}
public System.Collections.Generic.IEnumerable<string> GetAvailableChannelServices()
{
throw new NotImplementedException();
}
2) IEMailMessagingServices.cs
public interface IEMailMessagingService
{
MailMessage SendAskUs(AskUsViewModel model);
}
MvcMailer works fine without this addition (outside of Orchard), but I am trying to get everything working within Orchard.
I just cannot figure out what I am doing wrong. Any thoughts?
Sorry for excessive code.
IEmailMessaginService does not implement IDependency, so it can't be found by Orchard as a dependency. That's why it's null.

Resources