Java Runnable, Callable, Supplier, etc.

Java doesn’t have the concept of delegate; instead, if you need a pointer to a function, you can create inline anonymous classes (or lambda expressions as of Java 8 ) which are implementations of certain interfaces designed for this propose (a.k.a functional interfaces as of Java 8). However as Java evolves, more of such interfaces are being added. While they may seem very similar and confusing, each of them has a unique characteristic which sets it apart from the others. You can map many of them to identical types in .NET. The following table lists some of the famous interfaces but there are many more. For example to support functions with two arguments, Java has another interface called BiFunction and if you need more arguments, you have to create your own interfaces. Remember that Java (up to version 10) doesn’t support identical class names if the only difference is number of type arguments. (In .NET there are various Func and Action types with up to 16 type arguments.)

Java Supports Exceptions Argument Returns .NET Equivalent
Callable<R>  Yes  No  Yes  Func<R>
Supplier<R>  No  No  Yes  Func<R>
Provider<T>  No  Yes  Yes  Func<T,T>
Function<T,R>  No  Yes  Yes  Func<T,R>
Consumer<T>  No  Yes  No  Action<T>
Runnable  No  No  No  Action

 

Advertisements

API Security Checklist

  • Use HTTPS to protect sensitive data (authentication credentials, API Keys, etc.) in transit.
  • Authentication/Authorisation: Make sure the endpoints are protected with proper access levels.
  • GET methods are an easy target for attackers. So never perform an operation that changes the state of your application in a GET method.
  • Protect your API agains CSRF (Cross-Site Request Forgery) attacks.
  • Make sure your API is not vulnerable to XSS (Cross-Site Scripting) attacks.
  • Sign JWT (JSON Web Tokens) securely preferably using secrets.
  • Use API Keys for every request.
  • Treat Management Endpoints differently than normal ones, by enforcing stronger security policies (e.g. multi-factor authentication.
  • Handle exceptions decently so that technical error details are not exposed to clients.
  • Use SOP (Same-Origin Policy) and disable CORS if it’s not needed. When enabling CORS, be as specific as possible.
  • Do not put any sensitive information in the URL params as they can be logged by servers. Put them in the request header or body.
  • When setting cookies, use Secure and HttpOnly. Also restrict the scope of cookies.
  • Any input or data being imported may eventually end up in users’s browsers as part of an HTML page and you don’t want to send a malicious script to the them. Validating input and imported data is one of the ways to prevent clickjacking, XSS or stored CSRF flaws.
  • Any input or data being imported may also end up being inserted into your database, so make sure your application is protected against SQL Injection attacks.
  • Set response Content-Type header properly to mach the response MIME type and disable MIME type sniffing (nosniff).

Install .NET Core on Ubuntu 15.10

Currently .NET Core is only supported for Ubuntu 14.04 and when you try installing it on Ubuntu 15.10 you get the following error:

Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 dotnet : Depends: libicu52 (>= 52~m1-1~) but it is not installable
E: Unable to correct problems, you have held broken packages.

As a workaround you can download and install libicu52 manually before installing dotnet .

How big can a class be

A class should be so small that anyone can easily understand it. If a method is not required to be understood in order to understand the class, then that method probably doesn’t belong in there and it’s not part of the responsibility of that class.

It is not a good idea to judge how big a class is based on the number of lines of code, however the following list can give you a rough estimate:

  • Less than 100: Ideal
  • 100 to 200: Ok
  • 200 to 300: Warning
  • 300+: The class is too big

How to install JSon.Net NuGet package

Problem:

You are going to install Json.NET NuGet package but you get the following error:

 JSON Failed to initialize the PowerShell host. If your PowerShell execution policy setting is set to AllSigned, open the Package Manager Console to initialize the host first.

Solution:

  1. Open PowerShell console in Administrator mode. Note that you should open the x86 version if you are running the 32bit version of Visual Studio. Likewise open x64 if your Visual Studio is 64bit.
  2. Run this command start-job { Set-ExecutionPolicy Unrestricted } -RunAs32 | wait-job | Receive-Job
  3. Reopen Visual and the issue should be fixed